2011-06-04 36 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);它功能更強大,可配置,但需要更多的工作。