• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get response code and message when using jersey client

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Abhijeet,

Please check your private messages.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic