【问题标题】:Spring . Rest Assured春天 。放心
【发布时间】:2016-07-26 16:43:25
【问题描述】:

API 签名:

 @RequestMapping(value = "uploadImage", method = RequestMethod.POST, produces = {"application/sd-service", "application/json"})

@ResponseBody

public ImageUploadResponse uploadImage(@RequestParam(value = "channel", required=false) Channel channel, @RequestParam(value = "file") MultipartFile file, @RequestParam(value = "responseProtocol")Protocol responseProtocol) 

我用放心发送如下请求上传图片文件。

Response res=RestAssured

                .given()
                .contentType("image/jpeg"
                .body("{"responseProtocol":"PROTOCOL_JSON","channel":"WEB"}")
                .multiPart(fileItem)
                .when()
                .log()
                .all()
                .post("http://x.y.z.a:8080/service/contactus/v2/uploadImage")
                .andReturn();

其中 String userDir = System.getProperty("user.dir");

File fileItem= new File(userDir +"//src//main//resources//1.jpeg");

是图像文件。但是由于下面提到的错误没有结果:

Getting org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: The current request is not a multipart request for below case:

【问题讨论】:

  • 你可以试试 .contentType("multipart/form-data")

标签: spring rest spring-mvc rest-assured


【解决方案1】:
res = RestAssured
            .given()
            .contentType("multipart/form-data")
            .accept("application/json")
            .formParameter("channel", "WEB")
            .formParameter("responseProtocol", "PROTOCOL_JSON")
            .body(request.getBody())
            .multiPart("file",fileItem,"image/jpeg")
            .when()
            .log()
            .all()
            .post(request.getURI())
            .andReturn();

使用表单参数解决它。

【讨论】:

    猜你喜欢
    • 2011-09-23
    • 1970-01-01
    • 2011-11-28
    • 2020-10-13
    • 2016-05-25
    • 2014-08-22
    • 2013-11-02
    • 2014-03-13
    • 2019-05-15
    相关资源
    最近更新 更多