Java 发送POST请求时发生org.springframework.http.converter.httpMessageNodeReadableException
我有一个带有以下控制器的Spring应用程序:Java 发送POST请求时发生org.springframework.http.converter.httpMessageNodeReadableException,java,spring,jackson,Java,Spring,Jackson,我有一个带有以下控制器的Spring应用程序: @RestController @RequestMapping("/app") public class RegisterRestController { @Autowired UserRepository userRepository; @Autowired PasswordEncoder passwordEncoder; @Autowired UserService userServ
@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();
}
但是,当我从邮递员发送包含以下内容的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安全性。服务器没有显示任何错误,控制器似乎没有收到请求,因为没有打印“内部”。我正在努力熟悉Spring,但是我找不到这样一个错误的原因。如果有任何帮助,我将不胜感激。提前感谢您的代码中有两个问题:
@RequestBody字符串RequestBody
{
“电话号码”:“9123456789”,
“密码”:“密码”
}
解决方案:
为需要登录的值创建一个类:
公共类登录{
公共字符串电话号码;
公共字符串密码;
//您需要一个零参数构造函数
//也许你必须添加getter和setter
}
更改控制器方法,使其需要此类型的对象
@RequestBody登录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
}
然后
你现在可以请邮递员来试试。Goodluck除上述原因外,还有一个原因是字段类型不匹配。 在我的例子中,字段被声明为UUID,我将其作为字符串发送。
将其作为UUID发送解决了我的问题。与我之前编写的代码类似的代码在另一个应用程序中工作过。然而,你的解决方案是完美的,它为我做到了。非常感谢。
@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();
}