You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Sunil Dhage <su...@coreobjects.com> on 2010/06/09 09:40:41 UTC

Problem with Exception handling in Jaxrs web services.

Hi, All the members of the users list,

I am a newbie to the users list and my issue is regarding exception handling in Rest web services.

I am trying to handle exceptions in my Resource methods as mentioned in http://cxf.apache.org/docs/jax-rs.html#E
My code is

    @Path("allEmployees/{noOfEmps}")
    @GET
    @Produces("application/xml")
public Employees getAllEmployees(int noOfEmps) {
        if (noOfEmps == 0){
            throw new WebApplicationException(403);
        }
        return null;
    }

 @Path("addEmployees/{empId}")
    @PUT
    public Response addEmployees(int empId){
        if (empId == 0){
            return Response.status(403).build();
        }
        return Response.ok().build();
    }

When I test using my browsers neither of the service methods come with the response code 403.
And when I generate my wadl by http://<host>/app/....?_wadl<http://%3chost%3e/app/....?_wadl>,
.......
<method name="GET">
<request></request>
<response>
<representation mediaType="application/xml"/>
</response>
</method>
.....
But it should show different response entries like

<response status ="403">
...
</response>
<response status = "200">
..
</response>

Then I tried mapping the exceptions as below

@Provider
public class ExceptionMapperProvider implements
        ExceptionMapper<WebApplicationException> {
    public Response toResponse(WebApplicationException ex) {
        return Response.status(ex.getResponse().getStatus()).
            entity(ex.getMessage()).
            type("text/plain").
            build();
    }
}

And registered this provider with the <jaxrs:providers> in my cxf.xml.

Still I am neither
 getting the response with status code 403
nor
WADL containing different response status codes.

Please help me in finding what am I missing out here.

Regards,
Sunil Kumar Dhage.

RE: Problem with Exception handling in Jaxrs web services.

Posted by "Willard, Jonathan" <Jo...@ca.com>.
There are some useful tools on the net for testing. You might want to
check out the RestClient at http://code.google.com/p/rest-client/ under
'Featured Downloads' on the right.  It is more useful than the browswer.

Jon


-----Original Message-----
From: Sunil Dhage [mailto:sunil.dhage@coreobjects.com] 
Sent: Thursday, June 10, 2010 2:46 AM
To: users@cxf.apache.org
Subject: RE: Problem with Exception handling in Jaxrs web services.

Thanks guys.

I got the problem solved.  What I was doing was "Testing using Mozilla
Firefox and it was not showing in any way the response status code."
My resolution steps are as follows:
1. First tested using telnet and sending HTTP Get invocation, I was
getting the expected resonse status code.
2. Then I used Internet Explorer and I got the response code as
expected,

Regards,
Sunil Kumar Dhage.

-----Original Message-----
From: Sunil Dhage [mailto:sunil.dhage@coreobjects.com] 
Sent: Wednesday, June 09, 2010 1:11 PM
To: users@cxf.apache.org
Subject: Problem with Exception handling in Jaxrs web services.

Hi, All the members of the users list,

I am a newbie to the users list and my issue is regarding exception
handling in Rest web services.

I am trying to handle exceptions in my Resource methods as mentioned in
http://cxf.apache.org/docs/jax-rs.html#E
My code is

    @Path("allEmployees/{noOfEmps}")
    @GET
    @Produces("application/xml")
public Employees getAllEmployees(int noOfEmps) {
        if (noOfEmps == 0){
            throw new WebApplicationException(403);
        }
        return null;
    }

 @Path("addEmployees/{empId}")
    @PUT
    public Response addEmployees(int empId){
        if (empId == 0){
            return Response.status(403).build();
        }
        return Response.ok().build();
    }

When I test using my browsers neither of the service methods come with
the response code 403.
And when I generate my wadl by
http://<host>/app/....?_wadl<http://%3chost%3e/app/....?_wadl>,
.......
<method name="GET">
<request></request>
<response>
<representation mediaType="application/xml"/>
</response>
</method>
.....
But it should show different response entries like

<response status ="403">
...
</response>
<response status = "200">
..
</response>

Then I tried mapping the exceptions as below

@Provider
public class ExceptionMapperProvider implements
        ExceptionMapper<WebApplicationException> {
    public Response toResponse(WebApplicationException ex) {
        return Response.status(ex.getResponse().getStatus()).
            entity(ex.getMessage()).
            type("text/plain").
            build();
    }
}

And registered this provider with the <jaxrs:providers> in my cxf.xml.

Still I am neither
 getting the response with status code 403
nor
WADL containing different response status codes.

Please help me in finding what am I missing out here.

Regards,
Sunil Kumar Dhage.


RE: Problem with Exception handling in Jaxrs web services.

Posted by Sunil Dhage <su...@coreobjects.com>.
Thanks guys.

I got the problem solved.  What I was doing was "Testing using Mozilla Firefox and it was not showing in any way the response status code."
My resolution steps are as follows:
1. First tested using telnet and sending HTTP Get invocation, I was getting the expected resonse status code.
2. Then I used Internet Explorer and I got the response code as expected,

Regards,
Sunil Kumar Dhage.

-----Original Message-----
From: Sunil Dhage [mailto:sunil.dhage@coreobjects.com] 
Sent: Wednesday, June 09, 2010 1:11 PM
To: users@cxf.apache.org
Subject: Problem with Exception handling in Jaxrs web services.

Hi, All the members of the users list,

I am a newbie to the users list and my issue is regarding exception handling in Rest web services.

I am trying to handle exceptions in my Resource methods as mentioned in http://cxf.apache.org/docs/jax-rs.html#E
My code is

    @Path("allEmployees/{noOfEmps}")
    @GET
    @Produces("application/xml")
public Employees getAllEmployees(int noOfEmps) {
        if (noOfEmps == 0){
            throw new WebApplicationException(403);
        }
        return null;
    }

 @Path("addEmployees/{empId}")
    @PUT
    public Response addEmployees(int empId){
        if (empId == 0){
            return Response.status(403).build();
        }
        return Response.ok().build();
    }

When I test using my browsers neither of the service methods come with the response code 403.
And when I generate my wadl by http://<host>/app/....?_wadl<http://%3chost%3e/app/....?_wadl>,
.......
<method name="GET">
<request></request>
<response>
<representation mediaType="application/xml"/>
</response>
</method>
.....
But it should show different response entries like

<response status ="403">
...
</response>
<response status = "200">
..
</response>

Then I tried mapping the exceptions as below

@Provider
public class ExceptionMapperProvider implements
        ExceptionMapper<WebApplicationException> {
    public Response toResponse(WebApplicationException ex) {
        return Response.status(ex.getResponse().getStatus()).
            entity(ex.getMessage()).
            type("text/plain").
            build();
    }
}

And registered this provider with the <jaxrs:providers> in my cxf.xml.

Still I am neither
 getting the response with status code 403
nor
WADL containing different response status codes.

Please help me in finding what am I missing out here.

Regards,
Sunil Kumar Dhage.

Re: Problem with Exception handling in Jaxrs web services.

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

The reason a WADL fragment is virtually empty is that Response is returned
and as far as the WADLGenerator is concerned it can not get any useful info
out of it.
Also, it is not possible to show individual status codes which may be set by
the application code in the java-first case. Only having @Oneway annotations
would result in WADL showing 202 status
You may want to try using @Description annotation to describe that a given
method may return a number of status codes.

cheers, Sergey
On Wed, Jun 9, 2010 at 8:40 AM, Sunil Dhage <su...@coreobjects.com>wrote:

> Hi, All the members of the users list,
>
> I am a newbie to the users list and my issue is regarding exception
> handling in Rest web services.
>
> I am trying to handle exceptions in my Resource methods as mentioned in
> http://cxf.apache.org/docs/jax-rs.html#E
> My code is
>
>    @Path("allEmployees/{noOfEmps}")
>    @GET
>    @Produces("application/xml")
> public Employees getAllEmployees(int noOfEmps) {
>        if (noOfEmps == 0){
>            throw new WebApplicationException(403);
>        }
>        return null;
>    }
>
>  @Path("addEmployees/{empId}")
>    @PUT
>    public Response addEmployees(int empId){
>        if (empId == 0){
>            return Response.status(403).build();
>        }
>        return Response.ok().build();
>    }
>
> When I test using my browsers neither of the service methods come with the
> response code 403.
> And when I generate my wadl by http://<host>/app/....?_wadl<http://
> %3chost%3e/app/....?_wadl>,
> .......
> <method name="GET">
> <request></request>
> <response>
> <representation mediaType="application/xml"/>
> </response>
> </method>
> .....
> But it should show different response entries like
>
> <response status ="403">
> ...
> </response>
> <response status = "200">
> ..
> </response>
>
> Then I tried mapping the exceptions as below
>
> @Provider
> public class ExceptionMapperProvider implements
>        ExceptionMapper<WebApplicationException> {
>    public Response toResponse(WebApplicationException ex) {
>        return Response.status(ex.getResponse().getStatus()).
>            entity(ex.getMessage()).
>            type("text/plain").
>            build();
>    }
> }
>
> And registered this provider with the <jaxrs:providers> in my cxf.xml.
>
> Still I am neither
>  getting the response with status code 403
> nor
> WADL containing different response status codes.
>
> Please help me in finding what am I missing out here.
>
> Regards,
> Sunil Kumar Dhage.
>