简体   繁体   English

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

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

I'm new to Spring...I have a Spring Boot API application and all of my methods (POST, GET, etc) work great with Postman when the Content-Type is set to application/json, I can send and receive JSON.我是 Spring 的新手...我有一个 Spring Boot API 应用程序,当 Content-Type 设置为 application/json 时,我的所有方法(POST、GET 等)都可以很好地与 Postman 配合使用,我可以发送和接收 JSON .

However, I'd really like for my methods to also accept a GET or POST in the browser.但是,我真的希望我的方法也能在浏览器中接受 GET 或 POST。 When I use the browser to do a GET, the API returns an INTERNAL_SERVER_ERROR.当我使用浏览器执行 GET 时,API 返回 INTERNAL_SERVER_ERROR。 I created a small form and try to POST to my API, but then I get the UNSUPPORTED_MEDIA_TYPE: Content type 'multipart/form-data;boundary=--------------------------802438220043016845671644;charset=UTF-8' not supported我创建了一个小表单并尝试发布到我的 API,但随后我得到了 UNSUPPORTED_MEDIA_TYPE: Content type 'multipart/form-data;boundary=-------------------- ------802438220043016845671644;charset=UTF-8' 不支持

These are 2 of the methods in my @RestController:这些是我的@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;
}

If there's any other code that would be helpful to show, let me know and I'll update the thread.如果有任何其他有助于显示的代码,请告诉我,我会更新线程。

In createModel method, instead of @RequestBody, please use @ModelAttribute for MyModelDto parameter.在 createModel 方法中,请使用 @ModelAttribute 作为 MyModelDto 参数,而不是 @RequestBody。

You can use can try following ways,您可以使用可以尝试以下方式,

Set consume block in "@RequestMapping".在“@RequestMapping”中设置消费块。

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

Use @Multipart annotation and file object as @Part annotation使用@Multipart注解和文件对象作为@Part注解

Instead of use @RequestBody use @RequestPart.而不是使用@RequestBody 使用@RequestPart。

暂无
暂无

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

相关问题 Java Spring:不支持内容类型“multipart/form-data;boundary;charset=UTF-8” - Java Spring: Content type 'multipart/form-data;boundary ;charset=UTF-8' not supported 内容类型 'multipart/form-data;boundary=----...;charset=UTF-8' 不支持 - Content type 'multipart/form-data;boundary=----...;charset=UTF-8' not supported Spring Boot - 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8” - Spring Boot - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported Spring 启动 controller 调用不支持内容类型 'application/json;charset=UTF-8' - Content type 'application/json;charset=UTF-8' not supported with Spring boot controller call spring boot mvc - 不支持内容类型'application / json; charset = UTF-8' - spring boot mvc - Content type 'application/json;charset=UTF-8' not supported UTF-8编码Java / Spring(多部分/表单数据) - UTF-8 encoding Java/Spring (Multipart/form-data) 使用 XML 数据时出错 内容类型 'application/xml;charset=utf-8' not supported spring - Error in consuming XML data Content type 'application/xml;charset=utf-8' not supported spring 当我尝试将 JSON 发送到 Spring 时,不支持内容类型“application/json;charset=UTF-8” - Content type 'application/json;charset=UTF-8' not supported, when i try send JSON to Spring Spring Rest 应用程序中的“不支持内容类型‘application/json;charset=UTF-8’” - "Content type 'application/json;charset=UTF-8' not supported" in Spring Rest application 内容类型 'application/x-www-form-urlencoded;charset=UTF-8' 不支持 - Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM