2013-01-16 123 views
14

我正在開發Spring MVC中的REST web服務。我需要改變jackson 2如何序列化mongodb objectids。我不知道該怎麼做,因爲我發現部分文檔傑克遜2,我所做的就是創建一個自定義序列:Spring 3.2和Jackson 2:添加自定義對象映射器

public class ObjectIdSerializer extends JsonSerializer<ObjectId> { 


    @Override 
    public void serialize(ObjectId value, JsonGenerator jsonGen, 
      SerializerProvider provider) throws IOException, 
      JsonProcessingException { 
     jsonGen.writeString(value.toString()); 
    } 
} 

創建ObjectMapper

public class CustomObjectMapper extends ObjectMapper { 

    public CustomObjectMapper() { 
     SimpleModule module = new SimpleModule("ObjectIdmodule"); 
     module.addSerializer(ObjectId.class, new ObjectIdSerializer()); 
     this.registerModule(module); 
    } 

} 

,然後註冊映射

<mvc:annotation-driven> 
    <mvc:message-converters register-defaults="true"> 
     <bean 
      class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
      <property name="objectMapper"> 
       <bean class="my.package.CustomObjectMapper"></bean> 
      </property> 
     </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 

我的CustomConverter永遠不會被調用。我認爲CustomObjectMapper定義是錯誤的,我將它從一些代碼改編爲jackson 1.x

在我的控制器中,我使用@ResponseBody。 我在哪裏做錯了?由於

+2

序列化和註冊看起來是正確的,所以我認爲問題在於xml配置。 – StaxMan

+1

是的,感謝您的建議,我在文件周圍有一個空的標籤。它正在工作 – alex

+0

僅供參考文檔說使用StdSerializer代替:http://fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/ser/std/StdSerializer.html – testing123

回答

0

我將如何做到這一點是:

創建註釋聲明你的定製序列:

@Target({ElementType.TYPE}) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface MyMessageConverter{ 
} 

設置組件掃描這在mvcconfiguration文件

<context:include-filter expression="package.package.MyMessageConverter" 
      type="annotation" /> 

和創建一個實現HttpMessageConverter<T>的類。

@MyMessageConverter 
public MyConverter implements HttpMessageConverter<T>{ 
//do everything that's required for conversion. 
} 

創建一個類extends AnnotationMethodHandlerAdapter implements InitializingBean

public MyAnnotationHandler extends AnnotationMethodHandlerAdapter implements InitializingBean{ 
    //Do the stuffs you need to configure the converters 
    //Scan for your beans that have your specific annotation 
    //get the list of already registered message converters 
    //I think the list may be immutable. So, create a new list, including all of the currently configured message converters and add your own. 
    //Then, set the list back into the "setMessageConverters" method. 
    } 

我相信這是您的目標所需要的一切。

乾杯。

+0

我相信你這樣做的方式,更多的是「澤西島的方式」。 – ninnemannk

+0

這個答案有很多不必要的複雜性。 OP正在做它正確的方式。 – Bart

2

您應該用@JsonSerialize annontation註釋相應的模型字段。你的情況可能是:

public class MyMongoModel{ 
    @JsonSerialize(using=ObjectIdSerializer.class) 
    private ObjectId id; 
} 

但在我看來,這應該最好不要使用實體模型與VOS。更好的方法是在它們之間建立不同的模型和映射。 你可以找到my example project here(我用Spring 3和Jackson 2的日期序列化作爲例子)。

0

沒有必要創建對象映射器。將jackson-core-2.0.0.jar和jackson-annotations-2.0.0.jar添加到您的項目中。

現在,下面的代碼行添加到您的控制器,而遞服務:

@RequestMapping(value = "students", method = RequestMethod.POST, headers = "Accept=application/json", consumes = "application/json") 

public HashMap<String, String> postStudentForm(
      @RequestBody Student student, HttpServletResponse response) 

不要錯過任何註釋。

相關問題