将数据绑定到RequestBody(415错误)

时间:2018-07-06 23:05:32

标签: java spring

我的控制器中有以下内容:

@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?

1 个答案:

答案 0 :(得分:0)

您正在请求中使用标头值“ application / x-www-form-urlencoded; charset = UTF-8”,而应使用“ application / json”