【发布时间】:2018-08-19 23:00:45
【问题描述】:
我是 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;
}
如果有任何其他有助于显示的代码,请告诉我,我会更新线程。
【问题讨论】:
-
你好,你有什么解决办法吗?我也面临同样的问题。
-
@JigarShah:不幸的是,没有,还没有解决方案:(我不得不继续我的其他一些项目。
-
这个问题已经在这里回答了stackoverflow.com/a/49736794/3407480
标签: java spring-boot