无法读取HTTP消息。缺少必需的请求正文

时间:2017-04-26 16:43:20

标签: java android spring http

我遇到了一个问题,我无法在服务器端获取我的对象。 我收到了这个错误:

128870 [http-apr-8080-exec-1] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.webserverconfig.user.entity.User com.webserverconfig.user.controller.UserController.login(com.webserverconfig.user.entity.User)

我正在尝试使用GET请求在服务器端发送对象。我想使用这个对象来验证它上面的字段。 (我正在使用简单的用户登录方法,我想检查userNameuserPassword)。

这是我在服务器端的代码: 请求:

  @RequestMapping(value = "/login", method = RequestMethod.GET)
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    public User login(@RequestBody User user)  {
        userValidator.validateUserLogin(user);
        securityService.autoLogin(user.getUserName(), user.getUserPassword());
        return user;
    }

实体用户:

@Entity
@Table(name = "Users")
public class User {
    public User() {
    }

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "Id", unique = true, nullable = false)
    private long id;

    @Column(name = "userName", nullable = false)
    private String userName;

    @Column(name = "userPassword", nullable = false)
    private String userPassword;

    @Transient
    private String confirmPassword;


    public long getId() {
        return id;
    }

    public String getUserName() {
        return userName;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }
}

客户端代码:

private class LoginUserTask extends AsyncTask<Void, Void, User> {
        @Override
        protected User doInBackground(Void... voids) {
            restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            User user = new User(userName, userPassword);
            return restTemplate.getForObject(URL.getUserLogin(), User.class, user);
        }

        @Override
        protected void onPostExecute(User user) {
            responseEntity = restTemplate.getForEntity(URL.getUserLogin(), String.class);
            HttpStatus responseStatus = responseEntity.getStatusCode();
            if(responseStatus.equals(HttpStatus.ACCEPTED)){
                view.makeToast("User login completed " + user.getUserName());
            } else {
                view.makeToast("User login or password is not correct");
            }
        }
    }

我错过了什么吗?请有人帮帮忙吗?

1 个答案:

答案 0 :(得分:4)

您已将User对象设置为@RequestBody注释作为输入参数。 在这种情况下,您必须在请求正文中使用POST方法和User对象。