2012-06-18 207 views
1

我使用Spring MVC的註解,以創建具有類似定義方法JSON REST API:我要排除或包括序列化的bean的屬性篩選屬性

@RequestMapping(value = "/authenticate", method = RequestMethod.POST) 
public @ResponseBody AuthenticationResponse authenticate(@RequestBody final DeviceInformation deviceInformation) 
      throws AuthenticationException { 
    return createAuthenticationResponse(deviceInformation, false); 
} 

對於處理不同的客戶端版本通過使用註釋像

class AuthenticationResponse { 
    @InterfaceVersion(max = 2) 
    String old; 

    @InterfaceVersion(min = 3) 
    String new; 
} 

因此,如果客戶端InterfaceVersion 2呼籲他將不會得到財產,如果他有3個電話,他就不會得到財產。

我已經發現傑克遜圖書館(這是由JSON使用的Spring)提供的功能,如JsonView,JsonFilter等,但我無法弄清楚我在哪裏以及如何配置這些東西。

回答

1

我使用@JsonFilter來選擇性地過濾同一個對象上的屬性,Spring並沒有(現在仍然不支持?),所以我註冊了一個自定義的MappingJackson2HttpMessageConverter來檢查返回類型,如果它是一個過濾器包裝類型,我把過濾器和應用它。

public class JsonFilterAwareMappingJackson2HttpMessageConverter extends 
    MappingJackson2HttpMessageConverter { 

private boolean prefixJson = false; 

@Override 
public void setPrefixJson(boolean prefixJson) { 
    this.prefixJson = prefixJson; 
    super.setPrefixJson(prefixJson); 
} 

@Override 
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) 
     throws IOException, HttpMessageNotReadableException { 

    JavaType javaType = getJavaType(clazz); 
    try { 
     return this.getObjectMapper().readValue(inputMessage.getBody(), javaType); 
    } 
    catch (IOException ex) { 
     logger.error("Could not read JSON: " + ex.getMessage()); 
     throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex); 
    } 

} 

@Override 
protected void writeInternal(Object object, HttpOutputMessage outputMessage) 
     throws IOException, HttpMessageNotWritableException { 
    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); 
    ObjectMapper objectMapper = getObjectMapper(); 
    JsonGenerator jsonGenerator = 
      objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding); 

    // A workaround for JsonGenerators not applying serialization features 
    // https://github.com/FasterXML/jackson-databind/issues/12 
    if (objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) { 
     jsonGenerator.useDefaultPrettyPrinter(); 
    } 

    try { 
     if (this.prefixJson) { 
      jsonGenerator.writeRaw("{} && "); 
     } 

     objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); 
     if (object instanceof FilterAppliedJsonObject) { 
      FilterAppliedJsonObject viewObject = FilterAppliedJsonObject.class.cast(object); 
      objectMapper.setFilters(viewObject.getFilters()); 
      objectMapper.writeValue(jsonGenerator, viewObject.getObject()); 
     } else if (object == null) { 
      jsonGenerator.writeNull(); 

     } else { 
      objectMapper.writeValue(jsonGenerator, object); 
     } 
    } 
    catch (JsonProcessingException ex) { 
     logger.error("Could not write JSON: " + ex.getMessage()); 
     throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); 
    } 
} 

} 

我用java @config而不是XML配置,盡我所能,讓我做的:

@Override 
public void configureMessageConverters(
     List<HttpMessageConverter<?>> converters) { 
    converters.add(new JsonFilterAwareMappingJackson2HttpMessageConverter()); 
    super.configureMessageConverters(converters); 
} 

我認爲this post會幫助你,如果你想註冊這種在XML轉換