Andrew
Andrew

Reputation: 621

Failed to read HTTP message. Required request body is missing

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 userName and userPassword).

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

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

Upvotes: 1

Views: 18938

Answers (1)

Itzik Shachar
Itzik Shachar

Reputation: 754

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.

Upvotes: 4

Related Questions