Shashank Awasthi
Shashank Awasthi

Reputation: 347

Post nested Json to spring controller

Well I am trying to retrieve a nested json in spring controller and getting 400 (bad request) error.

JSON

{"AuthenticationInfo":
  {"loginId":"243324","password":"xyz"}
}

Controller

  @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+"}";
        }
    }

The error goes when I fire just simple Json and retrieve it accordingly. The only issue here is a nested structure of the Json

Upvotes: 3

Views: 10624

Answers (2)

dharam
dharam

Reputation: 8106

Now whatever I am going to say might sound stupid. I am not sure if this is the behavior only with jackson or any other JSON library as well.

It will work and I have tested it, you just need to change the case of the private property declared in MyAuthentication class. Use something like below:

private AuthenticationInfo authenticationInfo;

Now you also have to change the request to match the case, so use the below:

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

This works perfectly fine.

Upvotes: 1

Larry.Z
Larry.Z

Reputation: 3724

Try to modify MyAuthentication like this

    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's default jsoin properties start with a lowercase letter.

Upvotes: 3

Related Questions