【问题标题】:Spring Boot Content type 'multipart/form-data;boundary=--------------------------#;charset=UTF-8' not supported不支持 Spring Boot 内容类型'multipart/form-data;boundary=--------------------------#;charset=UTF-8'
【发布时间】:2018-08-19 23:00:45
【问题描述】:

我是 Spring 新手...我有一个 Spring Boot API 应用程序,当 Content-Type 设置为 application/json 时,我的所有方法(POST、GET 等)都可以很好地与 Postman 一起使用,我可以发送并接收 JSON。

但是,我真的希望我的方法也能在浏览器中接受 GET 或 POST。当我使用浏览器执行 GET 时,API 返回一个 INTERNAL_SERVER_ERROR。我创建了一个小表单并尝试发布到我的 API,但随后我得到了 UNSUPPORTED_MEDIA_TYPE: Content type 'multipart/form-data;boundary=-------------------- -------802438220043016845671644;charset=UTF-8' 不支持

这些是我的@RestController 中的两个方法:

@RequestMapping(method = RequestMethod.POST, value = {"","/"})
public ResponseEntity<MyModel> createModel(@Valid @RequestBody MyModelDto modelDto) {
    MyModel model = modelService.createModel(modelDto);
    URI createdModelUrl = ServletUriComponentsBuilder.fromCurrentRequest().path("/{identifier}")
            .buildAndExpand(model.getIdentifier()).normalize().toUri();
    return ResponseEntity.created(createdModelUrl).build();

@RequestMapping(method = RequestMethod.GET, value = "/{identifier}")
public Resource<MyModel> getByIdentifier(@PathVariable("identifier") String identifier) {
    MyModel model = modelService.getByIdentifier(identifier);
    Resource<MyModel> resource = new Resource<>(model);
    return resource;
}

如果有任何其他有助于显示的代码,请告诉我,我会更新线程。

【问题讨论】:

标签: java spring-boot


【解决方案1】:

在 createModel 方法中,请使用 @ModelAttribute 代替 @RequestBody 作为 MyModelDto 参数。

【讨论】:

    【解决方案2】:

    您可以使用可以尝试以下方式,

    在“@RequestMapping”中设置消费块。

    like , @RequestMapping(value="/abc", consume = "multipart/form-data", method=HTTPMethod.POST")

    使用@Multipart注解和文件对象作为@Part注解

    不要使用@RequestBody,而是使用@RequestPart。

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 1970-01-01
      • 2018-12-22
      • 2020-04-13
      • 2023-02-15
      • 2018-08-06
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多