交的嵌套JSON来弹簧控制器(Post nested Json to spring controll

2019-10-18 13:45发布

那么我想找回春天控制器嵌套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;
    }

MyAuthentication

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;
    }
}

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的嵌套结构

Answer 1:

现在无论我会说可能听起来很蠢。 我不知道这是只与杰克逊或任何其他的JSON库的行为也是如此。

它会工作,我已经测试过它,你只需要改变MyAuthentication类中声明私有财产的情况。 使用类似如下:

private AuthenticationInfo authenticationInfo;

现在,你也必须改变的情况相匹配的要求,因此使用下面:

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

这工作完全正常。



Answer 2:

尝试修改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;
    }
}

杰克逊的默认jsoin属性开始用小写字母。



文章来源: Post nested Json to spring controller