Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 发送POST请求时发生org.springframework.http.converter.httpMessageNodeReadableException_Java_Spring_Jackson - Fatal编程技术网

Java 发送POST请求时发生org.springframework.http.converter.httpMessageNodeReadableException

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

我有一个带有以下控制器的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();
}
但是,当我从邮递员发送包含以下内容的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,但是我找不到这样一个错误的原因。如果有任何帮助,我将不胜感激。提前感谢

您的代码中有两个问题:

  • 您尝试将JSON转换为控制器内的对象。 这在Spring之前已经完成了。它接收请求主体,并尝试将其转换为控制器方法中相应参数的Java类
  • 控制器方法需要一个字符串:
    @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();
    }