2016-04-15 55 views
5

我最近切換到澤西島2。 我通過文檔/網頁瞭解如何將響應類轉換爲自定義類使用.readEntity(ClassName.class);Jersey JSON從駱駝案件切換到下劃線(蛇案)

但我堅持使用CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES命名策略。

當前轉換失敗,因爲響應字段是「_」,我的POJO有Snake情況。

任何幫助將不勝感激。

在jersey1,我一直在做這樣的:

MyResponse myResponse = client 
     .resource(url) 
     .type(MediaType.APPLICATION_JSON) 
     .accept(MediaType.APPLICATION_JSON) 
     .post(RequestClass.class, request); 

我不能達到崗位的球衣2相同: 它提供了編譯時錯誤,當我在上面的代碼:

我也試過:

MyResponse myResponse = client 
     .target(getUrl()) 
     .request() 
     .post(Entity.entity(request, MediaType.APPLICATION_JSON)) 
     .readEntity(MyResponse.class); 

,但它不是創建myResponse對象,使我得到了Snake_case響應的反應,但我的POJO有駱駝領域。

+0

請顯示我真實的代碼 –

回答

5

這是需要與傑克遜ObjectMapper配置的東西。您可以在ContextResolver中執行此操作。基本上,你需要像

@Provider 
public class MapperProvider implements ContextResolver<ObjectMapper> { 
    final ObjectMapper mapper; 

    public MapperProvider() { 
     mapper = new ObjectMapper(); 
     mapper.setPropertyNamingStrategy(
       PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); 
    } 

    @Override 
    public ObjectMapper getContext(Class<?> cls) { 
     return mapper; 
    } 
} 

然後與你的客戶

client.register(MapperProvider.class); 

註冊如果您需要在服務器上的這種支持也,那麼你將需要註冊在服務器上了。

+0

這解決了它。謝謝:)不能upvote由於低信譽的答案:( – k1133

相關問題