Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数据绑定到请求主体(415错误)_Java_Spring - Fatal编程技术网

Java 将数据绑定到请求主体(415错误)

Java 将数据绑定到请求主体(415错误),java,spring,Java,Spring,我的控制器中有以下各项: @Controller public class GreetingController { @PostMapping("/register") public String registerUser(@RequestBody UserEntity request) throws ServletException, IOException { System.out.println(request.getId()); retu

我的控制器中有以下各项:

@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(从这个问题:)

此外,我能够使用
公共字符串注册器(HttpServletRequest request)
很好,但是当我尝试使用
@RequestBody
时,它只会给我这个错误


如何使@RequestBody成为用户实体?

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

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

您的设置似乎是正确的,忽略了正确的实体定义不是这样的事实,您将什么作为JSON从客户端发布到
registerUser
方法?@仪表板更新了上面的问题。好的,您是否将请求的
内容类型
头设置为
application/json
?因为spring在后台使用Jackson将JSON数据绑定到POJO。检查后请告诉我。尝试将
@Consumes(MediaType.APPLICATION\u JSON)
(或类似内容)添加到资源方法中。我猜Jackson不知道如何解析传入的请求正文,因为您没有指定它应该期望的内容。@ChocketandCheese要使用
@Consumes
装饰器,我需要导入什么?忽略正确的实体定义不是这样的事实,您的设置似乎是正确的,您将什么
作为JSON从客户端发布到
注册服务器
方法?@仪表板更新了上述问题。好的,您是否将请求的
内容类型
标题设置为
应用程序/JSON
?因为spring在后台使用Jackson将JSON数据绑定到POJO。检查后请告诉我。尝试将
@Consumes(MediaType.APPLICATION\u JSON)
(或类似内容)添加到资源方法中。我猜Jackson不知道如何解析传入的请求正文,因为您没有指定它应该期望的内容。@ChocolateAndCheese要使用
@Consumes
装饰器,我需要导入什么?