@JsonView注释如何用于嵌套实体? [英] How does @JsonView annotation can be used for nested entities?

查看:158
本文介绍了@JsonView注释如何用于嵌套实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的嵌套实体使用@JsonView注释。
为了更清楚,假设我们有2个实体,每个实体都有自己的视图类。

I am trying to use @JsonView annotation for my nested entities. To be more clear, assume that, we have 2 entities, each entity have its own view class.

public class JsonViewAddress {
     //some view classes
}


public class Address {
   //fields annotated by JsonViewAddress's classes and @JsonProperty
}

public class JsonViewPerson {
  //some view classes
}

public class Person {

 //some fields (yes annotated with JsonViewPerson classes and @JsonProperty)

 //also assume that this is annotated with any JsonViewPerson's class.
 private Address address;

}

让我们尝试用响应中的Json类型实现此Person类

Let's try to achieve this Person class with Json type from response

@Path("hey")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class Resource {


   @GET
   @Path("/stack/overflow")
   @JsonView(value = { /* WHAT SHOULD BE WRITTEN HERE ? */ })
   public Response method() {
       //return Person entity in response
   }
}

@JsonView注释采用String数组,但我该如何确定这些写入的视图类必须为它们所属的每个实体显式工作?我想看看UserView适用于User,AddressView适用于地址,不久。

@JsonView annotation takes String array, but how should i determine that these written view classes must be work explicitly for each entity they belonged ? I want to see that UserView works for User, AddressView works for Address, shortly.

谢谢。

推荐答案

我有类似的问题,这不是你的问题,但也许这个方法对你有用。

I had a similar problem, it's not exactly your problem but maybe the approach will be usefull for you.

我只使用一个ViewObject

I only use one ViewObject

public class Views {

    public static class Low {
    }

    public static class Medium extends Low {
    }

    public static class High extends Medium {
    }
}

每次我有一个嵌套对象时我都需要它在Views.Low视图中,所以我写了一个Serializer来做这个。

Every time that i have a nested object i need it in the Views.Low View, so i write a Serializer to do this.

public class Serializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writerWithView(Views.Low.class).writeValue(jgen, value);
    }
} 

最后在我使用的对象中我喜欢这样:

Finally in my objects i used like this:

public class Person {

   @JsonView(Views.High.class)
   @JsonSerialize(using = Serializer.class)
   private Address address;

}

资源:

@Path("hey")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class Resource {


   @GET
   @Path("/stack/overflow")
   @JsonView(Views.High.class)
   public Response method() {
       //return Person entity in response with address low view
   }

   @GET
   @Path("/stack/overflow")
   @JsonView(Views.Medium.class)
   public Response method() {
       //return Person entity in response with no address
   }

}

您可以使用此方法解决您的问题,但如果您使用不同的类视图,则需要编写大量的序列化器。

You can use this approach to your problem, but if you use different Class Views you have to write a lot of Serializers.

这篇关于@JsonView注释如何用于嵌套实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆