加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

发送POST请求时发生java-org.springframework.http.converter.Ht

发布时间:2020-12-15 01:43:20 所属栏目:大数据 来源:网络整理
导读:我有一个带有以下控制器的Spring应用程序: @RestController @RequestMapping("/app") public class RegisterRestController { @Autowired UserRepository userRepository; @Autowired PasswordEncoder passwordEncoder; @Autowired UserService userService

我有一个带有以下控制器的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 tokenn 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 tokenn at [Source: java.io.PushbackInputStream@eaa3acb; line: 1,column: 1]","path": "/app/loginuser"
}

另外,我也在试验Spring Security.服务器没有显示任何错误,控制器似乎没有收到请求,因为没有打印“内部”.我试图了解Spring,但是我找不到出现这种错误的原因.我将不胜感激任何帮助.提前致谢

最佳答案
您的代码中存在两个问题:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读