【问题标题】:Post nested Json to spring controller将嵌套的 Json 发布到 spring 控制器
【发布时间】: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


【解决方案1】:

尝试像这样修改MyAuthentication

    public static class MyAuthentication extends AuthenticationInfo {
    @JsonProperty("AuthenticationInfo")
    private AuthenticationInfo AuthenticationInfo;

    public IndexController.AuthenticationInfo getAuthenticationInfo() {
        return AuthenticationInfo;
    }

    public void setAuthenticationInfo(IndexController.AuthenticationInfo authenticationInfo) {
        AuthenticationInfo = authenticationInfo;
    }

    @Override
    public String toString() {
        return "AuthenticationInfo : " + AuthenticationInfo;
    }
}

Jackson 的默认 jsoin 属性以小写字母开头。

【讨论】:

  • 非常感谢,它成功了。我需要解决这个问题,因为我可以控制我们发出的其余请求
【解决方案2】:

现在无论我要说什么都可能听起来很愚蠢。我不确定这是否仅适用于 jackson 或任何其他 JSON 库。

它会起作用并且我已经测试过了,你只需要更改在 MyAuthentication 类中声明的私有属性的大小写。使用如下内容:

private AuthenticationInfo authenticationInfo;

现在您还必须更改请求以匹配大小写,因此请使用以下内容:

{"authenticationInfo":{"loginId":"abcsdsd","password":"adsfsdfsdbcsdsd"}}

这工作得很好。

【讨论】:

    猜你喜欢
    • 2011-08-19
    • 2016-07-05
    • 1970-01-01
    • 2012-10-03
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多