简体   繁体   English

避免在杰克逊运行时对某些字段进行序列化

[英]Avoid serialization of certain fields at runtime in Jackson

I have a controller which produces JSON, and from this controller, I return an entity object, which is automatically serialized by Jackson. 我有一个生成JSON的控制器,然后从该控制器返回一个实体对象,该对象将由Jackson自动序列化。

Now, I want to avoid returning some fields based on a parameter passed to the controller. 现在,我想避免基于传递给控制器​​的参数返回某些字段。 I looked at examples where this is done using FilterProperties / Mixins etc. But all the examples I saw requires me to use ObjectMapper to serialize / de-serialize the bean manually. 我查看了使用FilterProperties / Mixins等完成此操作的示例。但是我看到的所有示例都要求我使用ObjectMapper手动序列化/反序列化Bean。 Is there any way to do this without manual serialization? 没有手动序列化,有没有办法做到这一点? The code I have is similar to this: 我拥有的代码与此类似:

@RestController
@RequestMapping(value = "/myapi", produces = MediaType.APPLICATION_JSON_VALUE)
public class MyController {
    @Autowired
    private MyService myService;

    @RequestMapping(value = "/test/{variable}",method=RequestMethod.GET)
    public MyEntity getMyEntity(@PathVariable("variable") String variable){
        return myservice.getEntity(variable);
    }
}

@Service("myservice")
public class MyService {
    @Autowired
    private MyEntityRepository myEntityRepository;

    public MyEntity getEntity(String variable){
        return myEntityRepository.findOne(1L);
    }
}


@Entity  
@Table(name="my_table")
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyEntity implements Serializable {

    @Column(name="col_1")
    @JsonProperty("col_1")
    private String col1;

    @Column(name="col_2")
    @JsonProperty("col_2")
    private String col2;

    // getter and setters
}

Now, based on the value of "variable" passed to the controller, I want to show/hide col2 of MyEntity. 现在,基于传递给控制器​​的“变量”的值,我想显示/隐藏MyEntity的col2。 And I do not want to serialize/deserialize the class manually. 而且我不想手动对类进行序列化/反序列化。 Is there any way to do this? 有什么办法吗? Can I externally change the Mapper Jackson uses to serialize the class based on the value of "variable"? 我是否可以根据“变量”的值从外部更改Mapper Jackson用来序列化类的类?

Use JsonView in conjunction with MappingJacksonValue . JsonViewMappingJacksonValue结合使用。

Consider following example: 考虑以下示例:

class Person {
    public static class Full {
    }

    public static class OnlyName {
    }

    @JsonView({OnlyName.class, Full.class})
    private String name;

    @JsonView(Full.class)
    private int age;

    // constructor, getters ...
}

and then in Spring MVC controller: 然后在Spring MVC控制器中:

@RequestMapping("/")
MappingJacksonValue person(@RequestParam String view) {
    MappingJacksonValue value = new MappingJacksonValue(new Person("John Doe", 44));
    value.setSerializationView("onlyName".equals(view) ? Person.OnlyName.class : Person.Full.class);
    return value;
}

使用此注释并将值设置为null,它将不会被序列化:

@JsonInclude(Include.NON_NULL)

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

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