小编典典

将嵌套的Json发布到spring控制器

spring-mvc

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


阅读 287

收藏
2020-06-01

共1个答案

小编典典

现在,我要说的听起来似乎很愚蠢。我不确定这是否仅是杰克逊或任何其他JSON库的行为。

它将起作用,并且我已经对其进行了测试,您只需要更改MyAuthentication类中声明的private属性的大小写即可。使用如下所示的内容:

private AuthenticationInfo authenticationInfo;

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

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

这工作得很好。

2020-06-01