1

I've the below class:

public class PersonResponse {
    public static final class Profile {
        int id;
    }
    public static class Error {
        String message;
        int code;
    }
    String name;
    Profile profile;
    //getters/setters
}

to map JSON response such as following:

{
    "name" : "first last",
    "profile" : {
        "id" : 1234
    },
    "error" : {
        "message": "some random error",
        "code" : 404
    }
}

This works fine but I've one endpoint that returns just the Profile object or some error. Therefore the response could be:

 {
    "id" : 1234
 }

OR

{
  "message": "profile not found",
  "code" : 404
}

Is there any way to reuse the PersonResponse class in this case rather than adding an error object inside the Profile object too ?

3
  • Are you using Spring Boot ? Commented Aug 20, 2019 at 17:55
  • @EduardoEljaiek yes Commented Aug 20, 2019 at 18:30
  • 1
    I'm posting a solution using @JsonViews Commented Aug 20, 2019 at 18:44

1 Answer 1

3

Yes, It's possible you can do it using Jackson @JsonView.

First you must create a class for declaring your views.

    public class PersonResponseViews {

        public static class Person { }

        public static class Profile { }
    }

Then you must include these changes in PersonResponse class

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.annotation.JsonView;

    @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
    class PersonResponse {

        @JsonView(PersonResponseViews.Person.class)
        String name;

        @JsonView(PersonResponseViews.Person.class)
        Profile profile;

        @JsonView({
            PersonResponseViews.Person.class,
            PersonResponseViews.Profile.class
        })
        Error error;

        @JsonProperty("id")
        @JsonView(PersonResponseViews.Profile.class)
        int getProfileId() {
            int id = 0;

            if (profile != null) {
                id = profile.id;
            }

            return id;
        }

        @JsonView({
            PersonResponseViews.Person.class,
            PersonResponseViews.Profile.class
        })
        @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
        static class Error {

            String message;
            int code;
        }

        @JsonView(PersonResponseViews.Person.class)
        @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
        static class Profile {
            int id;
        }
    }

How to use your JSON views with a Spring Rest Controller

    @JsonView(PersonResponseViews.Person.class)
    @RequestMapping("/person")
    public @ResponseBody
    PersonResponse getPerson() {
        PersonResponse resp = new PersonResponse();  
        resp.name = "first last";
        resp.profile = new PersonResponse.Profile();
        resp.profile.id = 1234;
        resp.error = new PersonResponse.Error();
        resp.error.code = 404;
        resp.error.message = "some random error";
        return resp;
    }

    @JsonView(PersonResponseViews.Profile.class)
    @RequestMapping("/profile")
    public @ResponseBody
    PersonResponse getProfile() {
        PersonResponse resp = new PersonResponse();
        resp.profile = new PersonResponse.Profile();
        resp.profile.id = 1234;
        resp.error = new PersonResponse.Error();
        resp.error.code = 404;
        resp.error.message = "some random error";
        return resp;
    }

It works

Sign up to request clarification or add additional context in comments.

1 Comment

If this is useful to you, please, remember to up-vote it and maybe marking it as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.