3

好吧,我正在尝试在 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 的嵌套结构

4

2 回答 2

3

尝试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 属性以小写字母开头。

于 2013-08-31T03:58:33.493 回答
1

现在,无论我要说什么,都可能听起来很愚蠢。我不确定这是否只是杰克逊或任何其他 JSON 库的行为。

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

private AuthenticationInfo authenticationInfo;

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

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

这工作得很好。

于 2013-08-31T04:28:55.570 回答