简体   繁体   English

内部对象的JSON到Java映射?

[英]JSON to Java mapping of inner object?

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: 映射JSON响应,如下所示:

{
    "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. 这工作正常,但我有一个端点只返回Profile对象或一些错误。 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 ? 有没有办法在这种情况下重用PersonResponse类,而不是在Profile对象中添加一个错误对象呢?

Yes, It's possible you can do it using Jackson @JsonView . 是的,你可以使用Jackson @JsonView来做到这@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 然后,您必须在PersonResponse类中包含这些更改

    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 如何使用Spring Rest Controller使用JSON视图

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

有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM