Spring Boot RestTemplate多部分格式错误

时间:2017-05-04 08:12:12

标签: spring-boot multipartform-data resttemplate

我是Spring Boot的新手。我正在为GET和POST制作一个简单的应用程序 使用RestTemplate配置REST API。此API仅适用于Content-Type:multipart / form-data。

GET请求正常,但我的POST请求有问题(来自服务器的422错误)。

@Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            // Configure Authorization
            restTemplate.getInterceptors().add(
                    new BasicAuthorizationInterceptor( "admin", "password" ) );

            // Add Mapping converter
            MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
            mappingJackson2HttpMessageConverter.setSupportedMediaTypes( Arrays.asList( MediaType.MULTIPART_FORM_DATA ) );
            restTemplate.getMessageConverters().add( mappingJackson2HttpMessageConverter );

            // Send a dummy Business Configuration Object
            BusinessConfiguration businessConfigurationDummyObject = new BusinessConfiguration();
            log.info( "Post object: " + businessConfigurationDummyObject );
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType( MediaType.MULTIPART_FORM_DATA );

            HttpEntity<BusinessConfiguration> httpEntity = new HttpEntity( businessConfigurationDummyObject, httpHeaders );
            try {
                restTemplate.postForLocation("http://localhost:8180/v1/businessConfiguration/", httpEntity, BusinessConfiguration.class );
            } catch (RestClientException e ) {
                log.error( "POST error: " + e.getMessage() );
            }

            // Get and log it
            try {
                BusinessConfiguration businessConfiguration = restTemplate.getForObject(
                        "http://localhost:8180/v1/businessConfiguration/" + businessConfigurationDummyObject.getBaseId() + ":" + businessConfigurationDummyObject.getVersion(),
                log.info("Get object: " + businessConfiguration.toString());
            } catch (RestClientException e ) {
                log.error("GET error: " + e.getMessage());
            }
        };

业务配置对象是一个简单的对象,但它有一个要上载的文件:

@JsonIgnoreProperties(ignoreUnknown = false)
public class BusinessConfiguration {

    private File file = new File( "C:\\Users\\foo\\Documents\\foo_file.txt" );
    private String baseId = "dummyBaseId";
    private String version = "dummyVersion";
    private String description = "dummyDescription";
    private String dataType = "JSON";
    private String usageType = "DataGen";

    public BusinessConfiguration() {
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getBaseId() {
        return baseId;
    }

    public void setBaseId(String baseId) {
        this.baseId = baseId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getUsageType() {
        return usageType;
    }

    public void setUsageType(String usageType) {
        this.usageType = usageType;
    }

    @Override
    public String toString() {
        return "BusinessConfiguration: BaseId - " + baseId;
    }
}

使用邮递员编写POST请求是有效的。请求如下所示:

POST /v1/businessConfiguration HTTP/1.1
Host: localhost:8185
Connection: keep-alive
Content-Length: 568
Authorization: Basic YWRtaW46cGFzc3dvcmQ=
Postman-Token: 81f247bd-0b27-3179-6617-621c6f5f068c
Cache-Control: no-cache
Origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryrQ5CQKU27QnmS7e3
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: es-ES,es;q=0.8,en;q=0.6

------WebKitFormBoundaryrQ5CQKU27QnmS7e3
Content-Disposition: form-data; name="baseId"

SAP0001
------WebKitFormBoundaryrQ5CQKU27QnmS7e3
Content-Disposition: form-data; name="version"

1.0.1
------WebKitFormBoundaryrQ5CQKU27QnmS7e3
Content-Disposition: form-data; name="description"

configuration for testing
------WebKitFormBoundaryrQ5CQKU27QnmS7e3
Content-Disposition: form-data; name="dataType"

JSON
------WebKitFormBoundaryrQ5CQKU27QnmS7e3
Content-Disposition: form-data; name="usageType"

DataGen
------WebKitFormBoundaryrQ5CQKU27QnmS7e3--

但是对于我的申请,请求是如此不同:

POST /v1/businessConfiguration/ HTTP/1.1
Content-Type: multipart/form-data
X-B3-TraceId: 864dc73755abd157
X-B3-SpanId: 864dc73755abd157
X-B3-Sampled: 0
X-Span-Name: http:/v1/businessConfiguration/
Authorization: Basic YWRtaW46cGFzc3dvcmQ=
Content-Length: 176
Host: localhost:8185
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_121)
Accept-Encoding: gzip,deflate

{"file":"C:\\Users\\foo\\Documents\\foo_file.txt","baseId":"dummyBaseId","version":"dummyVersion","description":"dummyDescription","dataType":"JSON","usageType":"DataGen"}

我认为问题出在mappingJackson2HttpMessageConverter上,它没有正确构成边界

感谢!!!

0 个答案:

没有答案