我遇到了麻煩傑克遜混入使用時@ResponseBody爲嵌入式類型工作的工作爲嵌入式類型。我使用的是Spring MVC 3.0和jackson 1.8。傑克遜混入不Spring MVC中
我有一個名爲EventEntry的對象。該對象有一個屬性用戶,通過getUser方法返回類型User。我爲EventEntry和User設置了mixins。 mixins只包含大量的@JasonIgnoreProperties值。
當EventEntry被流傳輸時,該混入正確應用和許多屬性被忽略。但是,當作爲EventEntry對象一部分的User對象流出時,不會應用mixin,並返回所有屬性。下面
代碼:
public class EventEntry {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用戶類有很多的屬性,其中大部分我不希望返回的JSON對象的一部分。
在我的控制,我想我的混入添加到用戶如下:
@RequestMapping(value = "/event/view/{eventIdentifier}/entries/json", method = RequestMethod.GET)
public @ResponseBody List<EventEntry> viewMyEventsJson(HttpServletResponse response, @PathVariable("eventIdentifier") String eventIdentifier){
ObjectMapper mapper = new ObjectMapper();
SerializationConfig serializationConfig = mapper.getSerializationConfig();
serializationConfig.addMixInAnnotations(EventEntry.class, BasicEventEntryJsonMixin.class);
serializationConfig.addMixInAnnotations(User.class, BasicPublicUserJsonMixin.class);
List<EventEntry> eventEntryList = getEventEntries(eventIdentifier);
try {
mapper.writValue(response.getOutputStream(), eventEntryList);
} catch (IOException ex) {
logger.error(ex);
}
return null;
}
我已經添加了兩個混入,一個用於EventEntry,其他的用戶。和以前一樣,EventEntry包含一個getUser()方法。
兩者混入只包含一大堆@JsonIgnoreProperty值:
@JsonIgnoreProperties({"eventId","lastUpdatedOn","lastUpdatedBy"})
public class BasicEventEntryJsonMixin extends EventEntry{
//Empty by design
}
@JsonIgnoreProperties({"emailAddress","lastUpdatedOn","lastUpdatedBy"})
public class BasicPublicUserJsonMixin extends User {
}
爲EventEntry的混入正確應用,但對於用戶的混入並不 - 整個對象被流傳輸出來。
我對傑克遜的唯一配置是
<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Support JSON -->
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
不要混入不適用於嵌入對象或已配置錯誤我的東西?
另外,有沒有更合適的方法來實現我想要做的基本上是對一個視圖,通過視圖上決定哪些屬性應返回,那些不應該?