Java Spring Boot内容类型';多部分/表格数据;边界=--------------------------------------;字符集=UTF-8';不支持

Java Spring Boot内容类型';多部分/表格数据;边界=--------------------------------------;字符集=UTF-8';不支持,java,spring-boot,Java,Spring Boot,我是Spring新手……我有一个Spring Boot API应用程序,我的所有方法(POST、GET等)都可以与Postman配合使用。当内容类型设置为application/json时,我可以发送和接收json 但是,我确实希望我的方法也能在浏览器中接受GET或POST。当我使用浏览器执行GET时,API返回一个内部服务器错误。我创建了一个小表单并尝试发布到我的API,但随后我得到了不受支持的媒体类型:Content TYPE“multipart/form data”;边界=--------

我是Spring新手……我有一个Spring Boot API应用程序,我的所有方法(POST、GET等)都可以与Postman配合使用。当内容类型设置为application/json时,我可以发送和接收json

但是,我确实希望我的方法也能在浏览器中接受GET或POST。当我使用浏览器执行GET时,API返回一个内部服务器错误。我创建了一个小表单并尝试发布到我的API,但随后我得到了不受支持的媒体类型:Content TYPE“multipart/form data”;边界=------------------------------------802438220043016845671644;字符集=UTF-8'不受支持

以下是my@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;
}
@RequestMapping(method=RequestMethod.POST,值={“”“/”})
公共响应属性createModel(@Valid@RequestBody MyModelDto modelDto){
MyModel=modelService.createModel(modelDto);
URI createdModelUrl=ServletUriComponentsBuilder.fromCurrentRequest().path(“/{identifier}”)
.buildAndExpand(model.getIdentifier()).normalize().toUri();
返回ResponseEntity.created(createdModelUrl.build();
@RequestMapping(method=RequestMethod.GET,value=“/{identifier}”)
公共资源getByIdentifier(@PathVariable(“identifier”)字符串标识符){
MyModel model=modelService.getByIdentifier(标识符);
资源=新资源(模型);
返回资源;
}

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

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

您可以使用以下方法

在“@RequestMapping”中设置消耗块

比如,@RequestMapping(value=“/abc”,consume=“multipart/formdata”,method=HTTPMethod.POST”)

将@Multipart注释和文件对象用作@Part注释


不要使用@RequestBody use@RequestPart。

嘿,你有什么解决方案吗?我面临着同样的问题。@JigarShah:不幸的是,没有,还没有解决方案:(我不得不继续我的一些其他项目。这个问题已经在这里得到了回答。这是否回答了你的问题?