過濾掉與傑克遜性能過濾性能是非常簡單的:球衣/傑克遜 - 基於查詢參數
final FilterProvider filters = new SimpleFilterProvider().
addFilter(... the name of the filter ...,
SimpleBeanPropertyFilter.filterOutAllExcept(... enumeration of properties ...));
<object mapper>.writer(filters).writeValueAsString(... the bean ...);
我想這在我州的REST應用程序集成。 API用戶可以通過提供查詢字符串來過濾屬性:
https://the-api/persons?fields=name,age,location,gender
什麼是澤西島最優雅的方式?我可以很容易地在我的資源方法中執行上述操作,但這不知怎的會殺死澤西島的優雅。另外,我相信爲每個請求創建一個新的ObjectMapper都會有性能損失。
可以寫一個MessageBodyWriter
,其取出由UriInfo
上下文fields
查詢參數和序列化,同時施加一個濾波器基於所述fields
查詢參數JSON的實體。這是做這件事的最好方法嗎?像這樣:
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JustTesting implements MessageBodyWriter<Object> {
@Context
UriInfo uriInfo;
@Context
JacksonJsonProvider jsonProvider;
public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return MediaType.APPLICATION_JSON_TYPE.equals(mediaType);
}
public long getSize(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
return -1;
}
public void writeTo(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream) throws IOException, WebApplicationException {
final FilterProvider filters = new SimpleFilterProvider().addFilter("MyFilter", SimpleBeanPropertyFilter.filterOutAllExcept(uriInfo.getQueryParameters().getFirst("fields")));
jsonProvider.locateMapper(aClass, mediaType).writer(filters).writeValue(outputStream, object);
}
}
它似乎工作,但我不確定這樣做是否聰明。我是澤西圖書館的新手。
我正在處理類似的事情。我的解決方案實際上也涉及Orika(我想保存查找lazey集合)。因此,我實現了一個管理器,用於檢查字段上的自定義註釋(如@Basic)以確定它們是否可映射,以及是否應該對其進行過濾。 更具體地說,在澤西島一側,我正在嘗試使用com.fasterxml.jackson.jaxrs.cfg.ObjectWriterInjector來將我「MixedIn」的過濾器應用於Object.class。 – 2014-10-21 05:29:41