我遇到了麻烦杰克逊混入使用时@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>
不要混入不适用于嵌入对象或已配置错误我的东西?
另外,有没有更合适的方法来实现我想要做的基本上是对一个视图,通过视图上决定哪些属性应返回,那些不应该?