ivoruJavaBoy
ivoruJavaBoy

Reputation: 1357

Manage Exception with Rest CXF

it is just few time that I am approaching the rest service, I would like ask a simple question ... at least I hope it is simple!

I've this service:

@Override
public UserTO register(UserTO userTO) {
    UserTO user=null;
    try{
    user=presentationService.register(userTO);
    }
    catch(ConstraintViolationException ex)
    {
        throw new CustomException(ex.getMessage());
    }
    return user;

}

And i've this client:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String processRegistration(@ModelAttribute("userForm")UserTO user, Map<String, Object> model) {  
    UserTO userTO=null;
    String error="";
    Response resp;
    userTO=registerme(client, user);
}

this is the exception:

public class CustomException extends WebApplicationException {
public CustomException(String message) {
    super(Response.status(Response.Status.BAD_REQUEST)
        .entity(message).type(MediaType.TEXT_PLAIN).build());
}
}

I would like to know how to manage exceptions from client...

for xample when i call this service and get and error, from back end i've:

   2015-06-20 18:44:28 WARN  SqlExceptionHelper:143 - SQL Error: 1062, SQLState: 23000
   2015-06-20 18:44:28 ERROR SqlExceptionHelper:144 - Duplicate entry 'aa' for key 'login_UNIQUE'
   2015-06-20 18:44:28 INFO  LoggingOutInterceptor:233 - Outbound Message
   ---------------------------
   ID: 1
   Response-Code: 400
   Content-Type: text/plain
   Headers: {Content-Type=[text/plain], Date=[Sat, 20 Jun 2015 16:44:28        GMT]}
   Payload: Nn funziona! Duplicate entry 'aa' for key 'login_UNIQUE'; SQL        [n/a]; constraint [null]; nested exception is               org.hibernate.exception.ConstraintViolationException: Duplicate entry 'aa' for key 'login_UNIQUE'

and in client i've:

 [INFO] Starting scanner at interval of 3 seconds.
 [ERROR] /DisConnectionView/register
 javax.ws.rs.WebApplicationException 

and then connection refused.

I know that is a noob question but a little help would be great..

Maybe i could wrap the exception inside the response element that i can return with the service, but if i would like to return a simple UserTO, is there another way to manage the exception and get in the client the type or the message?

Thank you!

Upvotes: 1

Views: 2867

Answers (1)

Xstian
Xstian

Reputation: 8282

I suggest you to use below examples.

Server side javax.ws.rs.ext.ExceptionMapper

public class RuntimeExceptionRestMapper implements ExceptionMapper<RuntimeException> {

    public Response toResponse(RuntimeException exception) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
                //handle your response
                .type(MediaType.APPLICATION_JSON_TYPE)
                .entity(exception.getMessage())
                .build();
    }

}

server configuration XML

<jaxrs:server id="" address="">
    <jaxrs:serviceBeans>
        ....
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean class="package.RuntimeExceptionRestMapper" />
    </jaxrs:providers>
</jaxrs:server>

Client side org.apache.cxf.jaxrs.client.ResponseExceptionMapper

public class RestResponseExceptionMapper implements ResponseExceptionMapper<Exception> {

    public Exception fromResponse(Response r) {
        //throw you exception
        return new WebApplicationException(r.getStatus());
    }

}

client configuration XML

<jaxrs:client id="service" address="" serviceClass="">
    <jaxrs:providers>
        <bean class="package.RestResponseExceptionMapper" />
    </jaxrs:providers>
</jaxrs:client>

Upvotes: 2

Related Questions