Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.http.converter.HttpMessageNotReadableException when sending a POST request

I have a Spring application with the following controller:

   @RestController
   @RequestMapping("/app") 
   public class RegisterRestController {
   @Autowired
    UserRepository userRepository;

   @Autowired
   PasswordEncoder passwordEncoder;

   @Autowired
   UserService userService;


   @RequestMapping( value="/loginuser", method =RequestMethod.POST,produces="application/json")
    public String loginUser(@RequestBody String requestBody) {
    System.out.println("inside");
    JSONObject responseJsonObject = new JSONObject();
    String phonenumber;
    String password;
    try{
        JSONObject object = new JSONObject(requestBody);
        phonenumber = object.getString("phonenumber");
        password = object.getString("password");
        User user = userService.findByNumber(phonenumber);
        String sha256Password = passwordEncoder.encode(password);
        if(sha256Password.equals(user.getPassword())){
            responseJsonObject.put("response", "Login Successful");
        }
        else {
            responseJsonObject.put("repsonse", "Login failed");
        }
    }
    catch (Exception e){
        e.printStackTrace();
        try {
            responseJsonObject.put("response", "Invalid Credentials");
        } catch (JSONException e1) {
            e1.printStackTrace();
        }

    }
    return responseJsonObject.toString();
}

However, when I send a POST request from Postman containing :

    {
      "phonenumber":"9123456789",
      "password":"password"
  }

I get the following response:

    {
   "timestamp": 1456043810789,
   "status": 400,
    "error": "Bad Request",
    "exception":      "org.springframework.http.converter.HttpMessageNotReadableException",
    "message": "Could not read JSON: Can not deserialize instance of   java.lang.String out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@eaa3acb; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@eaa3acb; line: 1, column: 1]",
    "path": "/app/loginuser"
}

Also, I was experimenting with Spring Security as well. The server does not show any error and the controller does not seem to receive the request as "inside" is not being printed. I am trying to get acquainted with Spring, however I could not find the reason for such an error. I would be grateful for any help. Thanks in advance

like image 533
raptor123 Avatar asked Feb 21 '16 08:02

raptor123


1 Answers

There are two problems in your code:

  1. You try to convert the JSON into an object inside the controller. This is already done by Spring. It receives the body of the request and tries to convert it into the Java class of the according parameter in the controller method.
  2. Your controller method expects a single string: @RequestBody String requestBody

And you are sending an object with two properties:

{
    "phonenumber": "9123456789",
    "password": "password"
}

Solution:

Create a class for the values you need to login :

public class Login {
    public String phonenumber;
    public String password;
    // you need a zero argument constructor
    // maybe you have to add getter and setters
}

Change your controller method so it expects an object of this type

@RequestBody Login requestBody
like image 188
Stefan Isele - prefabware.com Avatar answered Oct 11 '22 13:10

Stefan Isele - prefabware.com