本文介绍了将带有 Spring RestTemplate 的 byte[] 上传到 SpringMVC rest 端点时出现 400 Bad Request的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将包含图像的字节 [] 作为 MultipartFile 上传到我的 Spring rest 服务(在 Spring Boot 中运行,顺便说一句),我的客户端运行 Spring RestTemplate 并收到 HttpClientErrorException: 400 Bad Request.

I am trying to upload a byte[] that contains an image to my Spring rest service (running in Spring Boot, btw) as a MultipartFile with my client running Spring RestTemplate and am getting HttpClientErrorException: 400 Bad Request.

我的端点:

@RequestMapping(value="/scale/{percent}", method= RequestMethod.POST)
public ResponseEntity scaleImage(@PathVariable("percent") float percent,
                                        @RequestParam("file") MultipartFile file) {

    try {
        if (!file.isEmpty()) {
            byte [] result = transformService.scaleImage(percent, file.getBytes());
            return getResponseEntityFromBytes(result);
        } else {
            return generateBadRequestError();
        }
    } catch (Exception e) {
        if (e instanceof InvalidOperationParameterException) {
            // TODO - populate message with bad parameters
            LOGGER.log(Level.FINE, "Invalid Parameters: ");
            return generateBadRequestError();
        } else {
            LOGGER.log(Level.SEVERE, "Exception caught: " + e.getMessage(), e);
            return generateServerError(e.getMessage());
        }
    }
}

我的 Spring RestTemplate 客户端:

My Spring RestTemplate client:

public void scaleImage(byte[] image, float percent) throws Exception {
    String url = "http://localhost:8080/scale/" + percent;
    this.testNumberThreads=10;
    this.testNumberThreads=10;

    MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<>();
    mvm.add("file", image);
    TransformedResponse r = doPost(url, mvm);
}

private TransformedResponse doPost(String url, MultiValueMap<String, Object> mvm) {
    RestTemplate restTemplate = new RestTemplate();
    TransformedResponse xr = null;
    try {
        xr = restTemplate.postForObject(url, mvm, TransformedResponse.class);
    } catch (RestClientException e) {
        e.printStackTrace();
    }
    return xr;
}

...

public class TransformedResponse {
    byte[] image;

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }
}

这是我在客户端看到的异常(还没有到达服务器):

Here is the exception I'm seeing in the client (nothing hitting server yet):

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:588)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:546)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:502)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:330)
    at com.me.image.xform.LoadTest.doPost(LoadTest.java:110)
    at com.me.image.xform.LoadTest.loadTestScalePercent(LoadTest.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:232)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)

为什么这个请求不能正确发布?

Why won't this request post correctly?

推荐答案

我发现了我的问题.我需要将 AbstractResource(在本例中为 ByteArrayResource)添加到我的 MultiValueMap 而不是原始字节数组.这是修复它的代码:

I found my problem. I needed to add an AbstractResource (in this case a ByteArrayResource) to my MultiValueMap instead of the raw byte array. Here's the code that fixed it:

public void scaleImage(byte[] image, float percent) throws Exception {
    String url = "http://localhost:8080/scale/" + percent;

    final byte[] rawBytes = image.clone();

    MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<>();
    ByteArrayResource bar = new ByteArrayResource(rawBytes) {
        @Override
        public String getFilename() {
            return "Test-"+rawBytes.length + ".jpg";
        }
    };

    mvm.add("file", bar);
    TransformedResponse r = doPost(url, mvm);
}

这篇关于将带有 Spring RestTemplate 的 byte[] 上传到 SpringMVC rest 端点时出现 400 Bad Request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:25