【问题标题】:JSON and MultipartFile in POST ControllerPOST 控制器中的 JSON 和 MultipartFile
【发布时间】:2017-10-13 06:22:32
【问题描述】:

我有一个控制器方法,它接受一个多部分的 post 请求,包含 json 和一个二进制文件。它确实是工作,但我想摆脱明确的objectMapper.readValue() 调用。

@PostMapping
public void createEmployee(@RequestParam("employee") String employee, 
                           @RequestParam("cv") MultipartFile cv
                          ) throws IOException {
    Employee empl = objectMapper.readValue(employee, Employee.class);
    service.createEmployee(empl, cv.getBytes());
}

我尝试了另一种方法:

@PostMapping
public void createEmployee(@RequestPart("employee") Employee employee, 
                           @RequestPart("cv") MultipartFile cv
                          ) throws IOException {
    service.createEmployee(employee, cv.getBytes());
}

但在这里我得到了代码 415:“不支持内容类型'application/octet-stream'”。

【问题讨论】:

    标签: spring-mvc jackson


    【解决方案1】:

    PostMapping注解中添加consume:

    例如:

    @PostMapping(consumes={"multipart/form-data"})
    

    【讨论】:

    • 试过了,但没有任何改变。
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多