2015-05-26 29 views
1

我有兩個使用Dropwizard-0.8實現的REST服務。 雙方分享以下POJO的API的依賴:Dropwizard/JerseyClient在發送http請求時忽略了JsonProperty

public class Report{ 
    private String text; 
    @JsonProperty("t") 
    public String getText() 
    { 
    return text; 
    } 

    public void setText(String tx) 
    { 
    text = tx; 
    } 
} 

我的服務器有一個休息求助:

@POST 
@Consumes(MediaType.APPLICATION_JSON + ";charset=UTF-8") 
@Produces(MediaType.TEXT_PLAIN + ";charset=UTF-8") 
@Timed 
public Response receive(Report dto) { 
    //do some stuff with dto 
} 

我的客戶有一個方法:

sendReport(report); 

有:

private void sendReport(Report report) { 
    final String uri = "http://localhost:8080/....."; 
    Response response = null; 
    try { 
     response = client.target(uri).request().post(Entity.entity(report, MediaType.APPLICATION_JSON), Response.class); 

     final int status = response.getStatus(); 
     if (status != Status.ACCEPTED.getStatusCode()) { 
      final StatusType statusInfo = response.getStatusInfo(); 
      throw new SomeException(); 
     } 

    } 
    catch (Exception e) { 
     LOGGER.error(e.getMessage()); 
    } 
    finally { 
     if (response != null) { 
      response.close(); 
     } 
    } 
} 

客戶在Dropwizard應用程序類由具有:

service.client = new JerseyClientBuilder(env).using(conf.getJerseyClient()).withProvider(JacksonJaxbJsonProvider.class).build(getName()); 
    env.jersey().register(service); 

其中「服務」是我的休息類調用「sendReport」的方法。

問題

當我打電話給我的服務器的其它服務從瀏覽器或捲曲等它完全與下列消息體預計:

{「T」:「一些文字的服務器「}

但是,當我運行我的應用程序來調用其餘服務時,我得到一個400」無法處理JSON「。 調試和日誌消息表明我的應用程序發送以下JSON到我的服務器:

{「文」:「服務器一些文本」}這導致了傑克遜不能找到一個錯誤

財產「文本」。

JerseyClient爲什麼忽略JsonProperty註釋?

+0

是否有可能你有兩個不同的傑克遜版本(即,codehaus和fasterxml)? –

+0

確實!另一個依賴是給我codehouse上的jackson-core-asl。我猜這個類加載器有問題。我會調查這一點。謝謝。 – HaVonTe

+0

dto類看起來很私人的getter/setter,只有getter而不是setter的jsonproperty。不能說這是因爲直接發生,但可能是相關的。 – Natan

回答

1

從我理解你使用的球衣的Entity.entity不知道@JsonProperty註釋(這是從傑克遜庫)。你需要做的是使用傑克遜庫進行序列化並將其發送給後期調用。

+0

我很驚訝,致電 Entity.entity(報告,MediaType.APPLICATION_JSON) 不會在內部調用Jackson提供程序,但現在我有一個解決方法。謝謝。 – HaVonTe