繁体   English   中英

不支持 Spring Boot 内容类型 'multipart/form-data;boundary=------------------------#;charset=UTF-8'

[英]Spring Boot Content type 'multipart/form-data;boundary=--------------------------#;charset=UTF-8' not supported

我是 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;
}

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

在 createModel 方法中,请使用 @ModelAttribute 作为 MyModelDto 参数,而不是 @RequestBody。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM