0
發送和接收PUT請求我跟着關於如何與新澤西
Post/Put/Delete http Json with additional parameters in Jersey + general design issues
的答案,但我繼續得到400 Bad Request
......不知道如何解決這個問題?
以下是我對球衣寧靜服務器:
@PUT
@Path("/insertmessage")
@Consumes({ MediaType.APPLICATION_JSON })
@Produces({ MediaType.TEXT_PLAIN })
public Response insertMessage(Message m) {
return Response.ok(m.toString(), MediaType.TEXT_PLAIN).build();
}
這裏是我的客戶:
ClientConfig config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
Client client = Client.create(config);
WebResource service = client.resource(getBaseURI());
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(new Message("a", "b", "message"));
System.out.print(service.path("insertmessage")
.type(MediaType.APPLICATION_JSON).put(String.class, json));
以下是消息的構造:
@JsonCreator
public Message(@JsonProperty("sender") String sender,
@JsonProperty("receiver") String receiver,
@JsonProperty("content") String content) {
this.sender = sender;
this.receiver = receiver;
this.content = content;
}
我有:
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
在我的web.xml
我的appology,這是一個錯字...我使用澤西島1.18。我的問題是如何發送一個對象到服務器,以及服務器如何接收這個對象。目前我使用傑克遜序列化對象,但它不斷顯示我400錯誤 – wwood 2014-11-02 19:29:00
如果你有** POJO_MAPPING **設置你不需要明確地映射對象。而且你甚至不需要註釋Message類。考慮到** getBaseURI()**返回正確的值,它應該工作。用捲曲或其他手控客戶端檢查服務器。 400意味着你在http請求本身中有錯誤,而不是在json中。我確實推薦Jersey 2.x,因爲1.x很麻煩。你不需要所有這些bug字符串文字,響應建設等 – Dmytro 2014-11-03 08:51:12
「你不需要明確地映射對象」,你的意思是我不需要使用傑克遜顯式序列化對象,而是直接發送對象本身呢? – wwood 2014-11-03 14:28:58