Quantcast

How to parse string from javax.ws.rs.core.Response?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to parse string from javax.ws.rs.core.Response?

cganesan

Hi

I'm using a simple rest client that executes a query and gets String response. I'm using WebClient.get(...) to execute the REST call as follows.

Response jsonResponse = oneViewWebClient.get();

I do see the response code of 200 in the jsonResponse object but unable to access the string value from the returned response. In this case, I'd like to parse the response using jsonResponse.getEntity(..) I'm not using JAXB to deserialize the object, since it's a string is there a simple method to parse the response?

Thanks
Chandru

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to parse string from javax.ws.rs.core.Response?

Sergey Beryozkin
Hi
On 26/03/12 16:49, Ganesan, Chandru wrote:
>
> Hi
>
> I'm using a simple rest client that executes a query and gets String response. I'm using WebClient.get(...) to execute the REST call as follows.
>
> Response jsonResponse = oneViewWebClient.get();
>
> I do see the response code of 200 in the jsonResponse object but unable to access the string value from the returned response. In this case, I'd like to parse the response using jsonResponse.getEntity(..) I'm not using JAXB to deserialize the object, since it's a string is there a simple method to parse the response?
>

String str = oneViewWebClient.get(String.class);
should do it, you can also get the Response:
oneViewWebClient.getResponse()

Cheers, Sergey

> Thanks
> Chandru
>
>


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to parse string from javax.ws.rs.core.Response?

cganesan
Thanks Sergey.

Is the WebClient thread-safe? I'm using a single instance of WebClient to service multiple requests.
Do I need to call the reset method on the webclient between successive calls?

-----Original Message-----
From: Sergey Beryozkin [mailto:[hidden email]]
Sent: Monday, March 26, 2012 11:01 AM
To: [hidden email]
Subject: Re: How to parse string from javax.ws.rs.core.Response?

Hi
On 26/03/12 16:49, Ganesan, Chandru wrote:
>
> Hi
>
> I'm using a simple rest client that executes a query and gets String response. I'm using WebClient.get(...) to execute the REST call as follows.
>
> Response jsonResponse = oneViewWebClient.get();
>
> I do see the response code of 200 in the jsonResponse object but unable to access the string value from the returned response. In this case, I'd like to parse the response using jsonResponse.getEntity(..) I'm not using JAXB to deserialize the object, since it's a string is there a simple method to parse the response?
>

String str = oneViewWebClient.get(String.class);
should do it, you can also get the Response:
oneViewWebClient.getResponse()

Cheers, Sergey

> Thanks
> Chandru
>
>


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: How to parse string from javax.ws.rs.core.Response?

cganesan
In reply to this post by Sergey Beryozkin
The problem with the following code is, if the resource is not found using the get call, it throws an IOException as follows. In this case I can't get the response from the webclient right after the get call. Any suggestions?

Caused by: java.io.IOException: HTTP response '404: Not Found'
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2187)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:2134)
        at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1988)


-----Original Message-----
From: Sergey Beryozkin [mailto:[hidden email]]
Sent: Monday, March 26, 2012 11:01 AM
To: [hidden email]
Subject: Re: How to parse string from javax.ws.rs.core.Response?

Hi
On 26/03/12 16:49, Ganesan, Chandru wrote:
>
> Hi
>
> I'm using a simple rest client that executes a query and gets String response. I'm using WebClient.get(...) to execute the REST call as follows.
>
> Response jsonResponse = oneViewWebClient.get();
>
> I do see the response code of 200 in the jsonResponse object but unable to access the string value from the returned response. In this case, I'd like to parse the response using jsonResponse.getEntity(..) I'm not using JAXB to deserialize the object, since it's a string is there a simple method to parse the response?
>

String str = oneViewWebClient.get(String.class);
should do it, you can also get the Response:
oneViewWebClient.getResponse()

Cheers, Sergey

> Thanks
> Chandru
>
>


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How to parse string from javax.ws.rs.core.Response?

Sergey Beryozkin
Hi,

On 26/03/12 18:26, Ganesan, Chandru wrote:
> The problem with the following code is, if the resource is not found using the get call, it throws an IOException as follows. In this case I can't get the response from the webclient right after the get call. Any suggestions?
>
> Caused by: java.io.IOException: HTTP response '404: Not Found'
> at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:2187)
> at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:2134)
> at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1988)
>
Have a look please at this page:
http://cxf.apache.org/docs/jax-rs-client-api.html

When the typed response is expected and the HTTP error code is returned
then the exception will be thrown. You can choose between writing a
typed code such as

try {
     String s = wc.get(String.class);
} catch (ServerWebApplicationException ex) {
     ex.getResponse();
}

or

Response r = wc.get();

You can register a ResponseReader provider to simplify reading the data
directly from Response.

As far as the thread safety is concerned: the simplest option is to
simply clone existing WebClient or proxy using WebClient factory
constructors, a very minor overhead will be added. Other options are
also available, please check the wiki,

wc.reset() is only needed to be called if the headers or queries used
during the previous call won't be needed for the next call.

HTH, Sergey

>
> -----Original Message-----
> From: Sergey Beryozkin [mailto:[hidden email]]
> Sent: Monday, March 26, 2012 11:01 AM
> To: [hidden email]
> Subject: Re: How to parse string from javax.ws.rs.core.Response?
>
> Hi
> On 26/03/12 16:49, Ganesan, Chandru wrote:
>>
>> Hi
>>
>> I'm using a simple rest client that executes a query and gets String response. I'm using WebClient.get(...) to execute the REST call as follows.
>>
>> Response jsonResponse = oneViewWebClient.get();
>>
>> I do see the response code of 200 in the jsonResponse object but unable to access the string value from the returned response. In this case, I'd like to parse the response using jsonResponse.getEntity(..) I'm not using JAXB to deserialize the object, since it's a string is there a simple method to parse the response?
>>
>
> String str = oneViewWebClient.get(String.class);
> should do it, you can also get the Response:
> oneViewWebClient.getResponse()
>
> Cheers, Sergey
>
>> Thanks
>> Chandru
>>
>>
>
>


--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com
Loading...