Unable to test the REST API developed with Spring Boot

I am trying to test the REST API created with Spring Boot. Following is the signature of the method:

@RequestMapping(consumes = "multipart/form-data", method = RequestMethod.POST)
    public Response<String> upload(@RequestBody CsvUploadModel form) {

Following is the details of Model Object:

private char separator;
    private char quoteCharacter;
    private String metricName;
    private String groupName;
    private MultipartFile file;
//getters and setters

I have tried accessing this service using 1. chrome Postman and 2. Simple http POST form. Every time I am getting the error: 415 : Unsupported media type.

EDIT: Following is the bean configuration for multi part bean:

/**
     * Allow file uploads
     *
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultiPartConfigFactory factory = new MultiPartConfigFactory();
        factory.setMaxFileSize("500MB");
        factory.setMaxRequestSize("500MB");
        return factory.createMultipartConfig();
    }

    /**
     * Get the multipart resolver
     *
     * @return
     */
    @Bean
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }

I tried changing @RequestBody to @RequestParam but it didn't work. Following is the request preview of postman.

POST /dev/wizard/upload HTTP/1.1
Host: localhost:10022
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="metricName"

test
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="separator"

,
----WebKitFormBoundaryE19zNvXGzXaLvS5C

Am I missing anything obvious?

Thanks


@RequestBody CsvUploadModel form

This requires a HttpMessageConverter to be present that can read request payloads of the type multipart/form-data . Unfortunately Spring currently does not provide such a converter. There is a FormHttpMessageConverter , but that can only read simple form data ( application/x-www-form-urlencoded ).

In order to get your method working you should remove the @RequestBody annotation and add a parameter for the files:

upload(CsvUploadModel form, @RequestParameter(required=false) MultipartFile file)

@RequestBody is not needed for binding form data. You then have to set the file manually:

form.setFile(file);

Maybe there's a third-party converter that supports reading multipart/form-data . Neither do I use nor know any.


Try retrofit

<dependency>
     <groupId>com.squareup.retrofit</groupId>
     <artifactId>retrofit</artifactId>
     <version>1.6.1</version>
</dependency>

.

 import retrofit.http.Body;
 import retrofit.http.POST;

 public interface IRestController {

      @POST("/api-name")
      public Response api(@Body Request request);
 }

.

 import static org.junit.Assert.assertNotNull;
 import org.junit.Test;
 import retrofit.RestAdapter;

 public class TestRestAPI {

       private static final String SERVER = "http://localhost:8080";

       private IRestController service = new RestAdapter.Builder()
                                            .setEndpoint(SERVER).build()
                                            .create(IRestController.class);

       @Test
       public void basicTest(){      
               Response response = service.api(new Request());
               assertNotNull(response);
       }
 }
链接地址: http://www.djcxy.com/p/1334.html

上一篇: cURL发布json数据,json数组和图像文件REST API测试

下一篇: 无法测试使用Spring Boot开发的REST API