You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sergey Beryozkin <se...@iona.com> on 2008/09/23 15:46:04 UTC

Re: JAX-RS:Setting returnvalue for custom types

Hi,

Just prepending a JAX-RS: prefix in the subject line.

> Hi,
>
> If I got a method like:
>
> Customer getCustomer(@PathParam('cust') String custId) {
>    Customer c = myCustomerDao.getCustomer(custId);
>
>    if (c == null) {
>        // do something
>    }
>    return c;
> }
>
> Now, in the do something clause, I might want to respond with a http
> code and maybe an errormessage. How do I inject those instead of just
> returning null ?

You can throw a JAX-RS WebApplicationException or use JAX-RS ExceptionMappers and throw any exception you like (including the 
RuntimeException), have a look at [1] please for more info :


Cheers, Sergey

[1] http://cwiki.apache.org/CXF20DOC/jax-rs-jsr-311.html

>
> Kind regards,
> Tarjei 

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Re: JAX-RS:Setting returnvalue for custom types

Posted by Sergey Beryozkin <se...@iona.com>.
Hi

There's an "Exception Handling" section there, and some basic examples are provided, but I'll add few more advanced 
examples. If 

new WebApplicationException(statusCode) 

is not good enough then you can use ResponseBuilder :

new WebApplicationException(Response.status(status).entity(someDescription).build());

Cheers, Sergey

> Hi,
> 
>> Just prepending a JAX-RS: prefix in the subject line.
> Ok.
>>
>>> Hi,
>>>
>>> If I got a method like:
>>>
>>> Customer getCustomer(@PathParam('cust') String custId) {
>>>    Customer c = myCustomerDao.getCustomer(custId);
>>>
>>>    if (c == null) {
>>>        // do something
>>>    }
>>>    return c;
>>> }
>>>
>>> Now, in the do something clause, I might want to respond with a http
>>> code and maybe an errormessage. How do I inject those instead of just
>>> returning null ?
>>
>> You can throw a JAX-RS WebApplicationException or use JAX-RS
>> ExceptionMappers and throw any exception you like (including the
>> RuntimeException), have a look at [1] please for more info :
> Thanks, I've been using that page quite a lot lately.
> 
> Would it be possible to edit the page and add an example of using
> WebApplicationException in the text - mainly to make it a bit more
> obvious how to use it. I skimmed across the paragraph refering to the
> exception a few times before I connected the dots. I tried to edit the
> page myself to add this.
> 
> Kind regards,
> Tarjei
> 
>>
>>
>> Cheers, Sergey
>>
>> [1] http://cwiki.apache.org/CXF20DOC/jax-rs-jsr-311.html
>>
>>>
>>> Kind regards,
>>> Tarjei 
>>
>> ----------------------------
>> IONA Technologies PLC (registered in Ireland)
>> Registered Number: 171387
>> Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland

Re: JAX-RS:Setting returnvalue for custom types

Posted by Tarjei Huse <ta...@scanmine.com>.
Hi,

> Just prepending a JAX-RS: prefix in the subject line.
Ok.
>
>> Hi,
>>
>> If I got a method like:
>>
>> Customer getCustomer(@PathParam('cust') String custId) {
>>    Customer c = myCustomerDao.getCustomer(custId);
>>
>>    if (c == null) {
>>        // do something
>>    }
>>    return c;
>> }
>>
>> Now, in the do something clause, I might want to respond with a http
>> code and maybe an errormessage. How do I inject those instead of just
>> returning null ?
>
> You can throw a JAX-RS WebApplicationException or use JAX-RS
> ExceptionMappers and throw any exception you like (including the
> RuntimeException), have a look at [1] please for more info :
Thanks, I've been using that page quite a lot lately.

Would it be possible to edit the page and add an example of using
WebApplicationException in the text - mainly to make it a bit more
obvious how to use it. I skimmed across the paragraph refering to the
exception a few times before I connected the dots. I tried to edit the
page myself to add this.

Kind regards,
Tarjei

>
>
> Cheers, Sergey
>
> [1] http://cwiki.apache.org/CXF20DOC/jax-rs-jsr-311.html
>
>>
>> Kind regards,
>> Tarjei 
>
> ----------------------------
> IONA Technologies PLC (registered in Ireland)
> Registered Number: 171387
> Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland