【发布时间】:2013-08-30 07:14:10
【问题描述】:
我正在尝试在 spring 控制器中检索嵌套的 json 并收到 400(错误请求)错误。
JSON
{"AuthenticationInfo":
{"loginId":"243324","password":"xyz"}
}
控制器
@RequestMapping(value = "/login", method = RequestMethod.POST,headers={"Accept=*/*","content-type=application/json"})
@ResponseBody
public MySubscriber getSubscriber(@RequestBody MyAuthentication myAuthentication) {
LOGGER.log(Level.INFO, "getSubscriber");
System.out.println("getSubscriber method : "+myAuthentication);
MySubscriber mySubscriber = helloWebService.getSubscriber(myAuthentication);
LOGGER.log(Level.INFO, "mySubscriber : " + mySubscriber);
System.out.println( "mySubscriber : " + mySubscriber);
return mySubscriber;
}
我的身份验证
public class MyAuthentication extends AuthenticationInfo {
private AuthenticationInfo AuthenticationInfo;
public AuthenticationInfo getAuthenticationInfo() {
return AuthenticationInfo;
}
public void setAuthenticationInfo(AuthenticationInfo authenticationInfo) {
AuthenticationInfo = authenticationInfo;
}
@Override
public String toString()
{
return "AuthenticationInfo : "+AuthenticationInfo;
}
}
身份验证信息
public class AuthenticationInfo {
private String loginId;
private String password;
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString()
{
return "{ loginId : "+loginId+" || password"+password+"}";
}
}
当我触发简单的 Json 并相应地检索它时,就会出现错误。这里唯一的问题是 Json 的嵌套结构
【问题讨论】:
-
也发布您的错误。
-
我正在使用 rest 客户端点击请求并收到此错误 HTTP 状态 400 - 类型状态报告消息描述客户端发送的请求在语法上不正确 ()。 JBoss Web/7.0.13.Final 控制台上没有任何内容
-
调试怎么样?你能做调试并检查哪一行导致错误。
-
我已经对其进行了调试并了解了@RequestBody 无法解析嵌套Json 的原因。如果我尝试解析一个普通的 json ,只有属性而不是对象,那么它可以正常工作
-
@RequestBody完全能够解析嵌套的 JSON,但是您必须遵守 Java Beans 规范。您的MyAuthentication没有字段AuthenticationInfo它有一个字段authenticationInfo。
标签: java json spring spring-mvc