• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get response code and message when using jersey client

 
abhijeet@bglr shrivastava
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am using com.sun.jersey.api.client.WebResource class to access a webservice.
Can someone please point me to the method by which i can get the response code and message returned by the web service.

currently i am using this method -
webResource.head().getResponseStatus().getStatusCode()

but it is always returning 404 code, which i guess is not currect.

Thanks,
Abhijeet
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch Abhijeet,

Please check your private messages.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "you guess"? It would be the correct status code if the URL you're trying to access did not exist. If it does exist, and the WS call returns something other than a 404, then it would be incorrect.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by abhijeet:
currently i am using this method -
webResource.head().getResponseStatus().getStatusCode()

but it is always returning 404 code, which i guess is not correct.


Basically you are invoking an HTTP HEAD request on the resource/URL and the server complains that the URL doesn't exist.

If you use Apache TCPMon (Tutorial) or java.net tcpmon it will be clearer to see what is going on as you can look at the raw HTTP requests/responses.
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a code snippet that I found in jersey/samples/entityprovider/src/test/java/com/sun/jersey/samples/entityprovider/MainTest.java
Hope this helps



so



should work.

Note that as far as I know JSR-311 is only a server API. The Jersey Client API was simply created for testing but is not part of JSR-311 which is why it lives in the proprietary com.sun.jersey.api.client package (it used to be in com.sun.ws.rest.api.client). This leaves it up to the client developer to decide what level of coupling to the server is appropriate.

So you may actually want to look into using something like the Jarkarta Commons HttpClient to access the resource on the server.
(Apparently Java SE 6 java.net.HttpURLConnection has improved considerably since J2SE 1.4.2 (Comparison)).

Now lets say that the representation is sent to you in XML or XHTML. Rather than doing a full blown XML-Schema validated parse on the document you may want to consider using XPath instead to just get at the few parts that are of interest to you. That way your client may actually continue to work even if other parts of the representation evolve. Its a little more complicated and requires a little more thought but that is the price of loose coupling.
[ November 11, 2008: Message edited by: Peer Reynders ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic