用於序列化視圖可以用下面的代碼來設置:
objectMapper.setConfig(objectMapper.getSerializationConfig().withView(DefaultView.class));
我結束了除去默認視圖。任何屬於專用視圖的字段,我繼續使用@JsonView(UserView.class/AdminView.class)進行註釋。任何我沒有想要序列化的字段都標記爲@JsonIgnore。
我利用以下ObjectMapper配置讓我想要的結果:
@Provider
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
private final ObjectMapper objectMapper;
public JacksonContextResolver() {
objectMapper = new ObjectMapper();
objectMapper.setConfig(objectMapper.getSerializationConfig().withView(Object.class));
objectMapper.enable(MapperFeature.DEFAULT_VIEW_INCLUSION);
}
@Override
public ObjectMapper getContext(Class<?> arg0) {
return objectMapper;
}
}