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

标签 java spring

我的 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/

相关文章:

java - ubuntu上的运行时执行

java - 如果不存在记录,如何创建一个删除表的事件和另一个事件

java - 数学公式未返回正确答案

java - 将 hbase 移植到 .net 上

java - 对特定登录客户的只读权限 | Spring MVC

java - Hibernate注解哪里只适用于1对多关系?

java - 做而行不通

java - 捕获 AuthenticationProvider 中抛出的异常

java - Hibernate 事务内自定义 id 生成

java - 我的 Spring Boot 服务器返回 406 Http 错误代码