开发者

Jersey Client forgets Accept headers on redirect

I'm trying to use Jersey as a client for a RESTful application. Specifically I'd like to POST some JSON to the server and get JSON back, so my code looks like this:

final JSONObject config = new JSONObject();
clientConfig.put("fooParam", 60 * 5); /* 5 min timeout */

final JSONObject newClient = client.resource(/* URL */).
                          type(MediaType.APPLICATION_JSON).
                          accept(MediaType.APPLICATION_JSON).
                          post(JSONObject.class, config);

this generates the expected HTTP request with the Content-Type and Accept headers set appropriately. Now the server decides the create the requested resource and redirects there using a HTTP/1.1 303 See Other response (which is good practice as far as I know). The good news is that Jersey happily picks up the Location header and indeed requests the resource it was told to. The bad thing is that it seems to have forgot t开发者_如何学Gohat I only wanted to get application/json resources and sends a GET with

Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2

which the server answers happily (with perfectly legal HTML). I verified this using Wireshark. The problem is that Jersey blows up on this as it can not parse this into a JSONObject. So my question is

  • Is that behaviour of Jersey correct or rather a bug?
  • Is there some clever way around this?

I'm aware that I could possibly go through Jersey's ClientResponse class and see if I was redirected myself, but I think there should be a better way to do this.


After a short conversation with Pavel on a Jersey mailing list, it seems as if the problem lies withing the HttpURLConnection class from the Java libraries. Working around this is easy by using the Apache HTTP Client library binding for Jersey, which works nicely.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜