我的 Controller 中有以下内容:
@Controller
public class GreetingController {
@PostMapping("/register")
public String registerUser(@RequestBody UserEntity request) throws ServletException, IOException {
System.out.println(request.getId());
return "register";
}
}
UserEntity
是:
@Entity
@Table(name = "users")
public class UserEntity {
private int id;
private String name;
private String email;
private String password;
我收到以下错误:
There was an unexpected error (type=Unsupported Media Type, status=415).
Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
请注意,我安装了 Jackson(来自此问题: Jackson Databind classpath issue )。
此外,我还可以使用 public String registerUser(HttpServletRequest request)
很好,但是当我尝试使用 @RequestBody
时它只是给了我这个错误。
如何让 @RequestBody 成为 UserEntity?
最佳答案
您在请求中使用 header 值“application/x-www-form-urlencoded;charset=UTF-8”,而应使用“application/json”
关于java - 将数据绑定(bind)到RequestBody(415错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51218595/