2011-06-04 34 views
1

我遇到了麻烦杰克逊混入使用时@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> 

不要混入不适用于嵌入对象或已配置错误我的东西?

另外,有没有更合适的方法来实现我想要做的基本上是对一个视图,通过视图上决定哪些属性应返回,那些不应该?

回答

5

的原因是你配置一个杰克逊映射器,但使用另一个(一个从春季)。

这样来做:

@RequestMapping(value = "/event/view/{eventIdentifier}/entries/json", 
       method = RequestMethod.GET) 
public @ResponseBody String viewMyEventsJson(
      @PathVariable("eventIdentifier") String eventIdentifier) 
      throws JsonGenerationException, JsonMappingException, IOException{ 
    ObjectMapper mapper = new ObjectMapper(); 
    SerializationConfig serializationConfig = mapper.getSerializationConfig(); 
    serializationConfig.addMixInAnnotations(EventEntry.class, 
              BasicEventEntryJsonMixin.class); 
    serializationConfig.addMixInAnnotations(User.class, 
              BasicPublicUserJsonMixin.class); 

    return mapper.writeValueAsString(getEventEntries(eventIdentifier)); 
} 

那么你甚至不需要XML配置

1

我不知道为什么混插会被忽略;有时框架使用代码生成,问题在于混合的目标不是由最终的实例实现的,但这似乎不太可能。 如果您可以验证Spring MVC之外也出现同样的问题,那么这可能是Jackson混入处理中的一个错误,所以也许首先要解决这个问题?

至于其他的方法,@JsonView是定义每次观看排除(参见http://wiki.fasterxml.com/JacksonJsonViews)最简单的方法。更新的选择是@JsonFilter(请参阅http://wiki.fasterxml.com/JacksonFeatureJsonFilter);它功能更强大,可配置,但需要更多的工作。