2012-10-04 43 views
5

我有一個基於Spring Data REST example project的工作項目,我試圖使用基於此wiki pageJackson module自定義序列化。註冊一個傑克遜模塊的春天數據REST

這裏是我的傑克遜模塊:

public class CustomModule extends SimpleModule { 
    public static Logger logger = LoggerFactory.getLogger(CustomModule.class); 

    public CustomModule() { 
     super("CustomModule", new Version(1, 0, 0, null)); 
    } 

    @Override 
    public void setupModule(SetupContext context) { 
     logger.debug("CustomModule.setupModule"); 
     SimpleSerializers simpleSerializers = new SimpleSerializers(); 
     simpleSerializers.addSerializer(new CustomDateTimeSerializer()); 
     context.addSerializers(simpleSerializers); 
    } 

} 

wiki page說:

你的ApplicationContext的範圍內聲明的任何模塊bean將通過出口被拾起並與其ObjectMapper註冊。

我對Spring仍然很陌生,所以我可能只是把我的模塊bean定義放在錯誤的地方;目前它在src/main/resources/META-INF/spring-data-rest/shared.xml,這是從repositories-export.xml進口:

<bean id="customModule" class="org.hierax.wpa.schema.mapping.CustomModule" /> 

我沒有看到日誌語句setupModule,但我確實看到登錄其他類輸出在同一個包。

我正在使用Spring Data REST 1.0.0.RC2。

+0

你有沒有找到一個解決這個? –

+0

不,但我問過Spring Data論壇,也許它會在那裏得到答覆:http://forum.springsource.org/showthread.php?130782-Spring-Data-REST-registering-a- Jackson-serial-serialization模塊 –

回答

4

我使用

在你已經鏈接到(雖然也許它,因爲這堆棧溢出後改變)維基條目中列出的解決方案在我的實例,我使用過成功彈簧數據休息 - [email protected]

您的代碼似乎是正確的,並且提供了正確加載您的應用程序上下文我沒有看到任何理由不工作。

我已經附上了我的簡單的模塊,它體現了使用日期格式的:

@Component 
public class JsonMarshallingConfigModule extends SimpleModule { 

    public JsonMarshallingConfigModule() { 
    super("JsonMarshallingConfigModule", new Version(1, 0, 0, "SNAPSHOT")); 
    } 

    @Override public void setupModule(SetupContext context) { 
    context.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'")); 
    } 
} 

也許它可以用來勾勒如果是逸岸傑克遜模塊,這是問題或彈簧DATA-休息-mvcweb。

+0

感謝您分享您的解決方案!我最近更新到[email protected]並遷移到示例項目中的基於代碼的配置,現在我已經在CustomModule上添加了@Component。 –

+0

似乎無法使其工作。組件被加載,我可以看到實例已經創建,但是,setupModule()沒有被調用。爲什麼? –