【问题标题】:Failed to read HTTP message. Required request body is missing无法读取 HTTP 消息。缺少所需的请求正文
【发布时间】:2017-04-26 16:43:19
【问题描述】:

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

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");
            }
        }
    }

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

【问题讨论】:

  • onPostExecute,但您正在检查get 请求?所以在你的 login 函数中找不到身体,你可能有 @RequestBody 注释?
  • @Nathan 我使用了错误的请求方法吗?我的意思是 - 获取
  • @Nathan 哦,我想我明白了。我应该在 getForEntity 方法中发送对象吗?
  • 它没有帮助我(我仍然收到这个错误
  • 我的意思是,在你的“requestMapping”中,你应该使用 POST 而不是 GET

标签: java android spring http


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 1970-01-01
  • 2019-03-29
  • 2017-08-01
  • 2019-10-01
相关资源
最近更新 更多