我是Spring的新手……我有一个Spring Boot API应用程序,我的所有方法(POST、GET等)都能很好地处理Postman当Content-Type设置为application/json时,我可以发送和接收JSON。
但是,我真的希望我的方法也能在浏览器中接受GET或POST。当我使用浏览器执行GET时,API返回一个INTERNAL_SERVER_ERROR。我创建了一个小表单,并尝试将其发布到我的应用程序接口中,但随后我得到了不支持的内容类型'multipart/form-data;boundary=--------------------------802438220043016845671644;charset=UTF-8‘:UNSUPPORTED_MEDIA_TYPE
下面是我的@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;
}如果有任何其他代码可以帮助显示,请让我知道,我会更新线程。
发布于 2019-01-19 20:14:35
在createModel方法中,请对MyModelDto参数使用@ModelAttribute,而不是@RequestBody。
发布于 2020-07-16 18:15:22
您可以尝试使用以下方法,
在"@RequestMapping“中设置消费块。
like,@RequestMapping(value="/abc",value== "multipart/form-data",method=HTTPMethod.POST")
使用@Multipart注解和文件对象作为@Part注解
使用@RequestPart代替@RequestBody。
https://stackoverflow.com/questions/49226517
复制相似问题