[英]org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing
[英]org.springframework.http.converter.HttpMessageNotReadableException when sending a POST request
我有一個帶有以下控制器的Spring應用程序:
@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();
}
但是,當我從Postman發送包含以下內容的POST請求時:
{
"phonenumber":"9123456789",
"password":"password"
}
我得到以下回應:
{
"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"
}
另外,我也在嘗試使用Spring Security。 服務器未顯示任何錯誤,並且由於未打印“內部”,控制器似乎未收到請求。 我正在嘗試熟悉Spring,但是找不到這種錯誤的原因。 我將不勝感激。 提前致謝
您的代碼中有兩個問題:
@RequestBody String requestBody
您將發送具有兩個屬性的對象:
{
"phonenumber": "9123456789",
"password": "password"
}
解:
為您需要登錄的值創建一個類:
public class Login {
public String phonenumber;
public String password;
// you need a zero argument constructor
// maybe you have to add getter and setters
}
更改您的控制器方法,以便它需要這種類型的對象
@RequestBody Login requestBody
Jackson庫將使用您在login User方法中定義的構造函數自動轉換為JSON。 因此,您無需轉換為json。 所以這意味着
{
"phonenumber": "9123456789",
"password": "password"
}
應該在您的構造函數中定義。 您應該已經定義了一個定義loginUser的實體類。
public class LoginUser{
String phonenumber;
String password;
// define all other variables needed.
public LoginUser(String phonenumber, String password){
this.phonenumber = phonenumber ;
this.password = password;
}
public LoginUser() {
//you need a default contructor. As srequired by spring
}
//Define the gettters and settters
}
然后
@RequestMapping( value="/loginuser", method = RequestMethod.POST,produces="application/json")
public String loginUser(@RequestBody LoginUser requestBody) {
System.out.println("inside");
try{
phonenumber = requestBody.getPhonenumber; // please define your getters and setters in the login class
password = requestBody.getpassword;
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();
}
您可以使用郵遞員立即嘗試。 祝好運
上面提到的原因之一可能是字段的類型不匹配。 在我的情況下,字段被聲明為UUID,我將其作為字符串發送。 將其作為UUID發送解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.