[英]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.