wood burning stoves*
The moose likes Web Services and the fly likes How to get response code and message when using jersey client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to get response code and message when using jersey client" Watch "How to get response code and message when using jersey client" New topic
Author

How to get response code and message when using jersey client

abhijeet@bglr shrivastava
Greenhorn

Joined: Nov 11, 2008
Posts: 1
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

Joined: Aug 19, 2005
Posts: 2922
    
    5
Welcome to JavaRanch Abhijeet,

Please check your private messages.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41627
    
  55
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.


Ping & DNS - my free Android networking tools app
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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

Joined: Aug 19, 2005
Posts: 2922
    
    5
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get response code and message when using jersey client