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
There are two problems in your code:
@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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With