gpt4 book ai didi

spring-mvc - 在 Spring MVC REST 中无法看到 JSON 转换错误

转载 作者:行者123 更新时间:2023-12-03 09:53:22 24 4
gpt4 key购买 nike

我正在做 AngularJS 和 Spring REST 应用程序。
这是我的代码。

  @RestController
@RequestMapping("/user")
// @Secured("ROLE_ADMIN")
public class UserController
{
@RequestMapping(value = "/verifyUser", method = RequestMethod.POST)
public Boolean verifyUser(@RequestBody User user)
{
}
}

user对象格式不正确,然后浏览器显示 400 (Bad Request)Eclipse 控制台中没有显示其他错误。如果 user,我只想看看反序列化中发生了什么确切错误对象的格式不正确。

最佳答案

像下面这样转换你的方法。使用 ObjectMapper 将 JSON 转换为对象,因此在转换时会抛出异常,您将能够识别问题。

@RequestMapping(value = "/verifyUser", method = RequestMethod.POST)
public Boolean verifyUser(@RequestBody String json)
{
try{
ObjectMapper om = new ObjectMapper();
User user = om.readValue(json, User.class);
}
catch(Exception e){
e.printStackTrace()
}
// Write your logic.....
return ....;

}

关于spring-mvc - 在 Spring MVC REST 中无法看到 JSON 转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com