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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43640063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:37:08  来源:igfitidea点击:

Failed to read HTTP message. Required request body is missing

javaandroidspringhttp

提问by Andrew

I have faced with a problem that I can't get my object on server side. I am getting this error:

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

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)

I am trying to send object on server side using GET request. I want to use this object just to verify fields on it. (I am doing simple user login method and i want to check userNameand userPassword).

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

Here is my code on server side: Request:

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

  @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 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;
    }
}

Client side code:

客户端代码:

private class LoginUserTask extends AsyncTask<Void, Void, User> {
        @Override
        protected User doInBackground(Void... voids) {
            restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingHymanson2HttpMessageConverter());
            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");
            }
        }
    }

Am I missing something? Can anybody help with this please ?

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

回答by Itzik Shachar

You have set a @RequestBody annotation with a User object as an input parameter. In this case, you have to use POST method along with a User object in the body of the request.

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