Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a 40X error with a custom message on JAX-RS Exception?

I'm working on a Web Service on JAX-RS that is already working. Now I'm looking for the way to catch some Exceptions in order to send an 40X error with a custom message to the user.

I have a web service, and an ExceptionMapper.

This is my web service:

@Path( value = "/test/")
public interface ServiceTest {

    @Path(value = "{rrf}")
    @GET
    @Produces(MediaType.TEXT_XML)
    public ObjectDTO getDealer(@PathParam("rrf") String rrf){
        ObjectDTO objectDTO = new ObjectDTO();
        if( verifyRRFSintax(rrf) ) {
            //Get the objet, this part works fine
        } else {
            throw new IllegalArgumentException("Custom message");
        } 
        return dwsDTO;
    }

    private boolean verifyRRFSintax(String rrf) {
        return rrf.matches("[0-9]{8}");
    }
}

This is my ExceptionMapper

@Provider
@Produces(MediaType.TEXT_XML)
public class IllegalArgumentExceptionMapper 
    implements ExceptionMapper<IllegalArgumentException> {

    @Override
    public Response toResponse(IllegalArgumentException e) {
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
}

and this is how it's registed on the application-context.xml file

<bean id="serviceTest" class="ServiceTest"/>

<jaxrs:server id="Server" address="/ws">
    <jaxrs:serviceBeans>
        <ref bean="serviceTest"/>
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <bean id="rffErrorException" class="IllegalArgumentExceptionMapper"/>
    </jaxrs:providers>
</jaxrs:server>

When I debug, the IllegalArgumentExceptionMapper catches the Exception I throw but I don't see the message on a yellow web page that is shown on the browser. I always have a

Erreur d'analyse XML : aucun élément trouvé / XML Parsing Error: no element found (in english)

How I can make to show this custom message on the browser? Why, even if I change the kind of Response Status (NOT_FOUND, BAD_REQUEST, FORBIDDEN), this yellow page is always the same?

PD: On the console I have in a message "out.handlemessage" that is printed when the Mapper catch the exception.

Thanks.

like image 732
jomaora Avatar asked Nov 03 '11 16:11

jomaora


People also ask

How do you handle exceptions in JAX RS?

Thrown exceptions are handled by the JAX-RS runtime if you have registered an exception mapper. Exception mappers can convert an exception to an HTTP response. If the thrown exception is not handled by a mapper, it is propagated and handled by the container (i.e., servlet) JAX-RS is running within.

How do I print a custom exception message?

You can print the exception message in Java using one of the following methods which are inherited from Throwable class. printStackTrace() − This method prints the backtrace to the standard error stream. getMessage() − This method returns the detail message string of the current throwable object.

What is exception mapper?

ExceptionMapper is a contract for a provider that maps Java exceptions to Response object. An implementation of ExceptionMapper interface must be annotated with @Provider to work correctly.

How do you handle exceptions in Jersey?

Approach 2 : Annotating a class with @ControllerAdvice and define methods with @ExceptionHandler. This is similar to Controller based exception (refer approach 1) but this is used when controller class is not handling the exception. This approach is good for global handling of exceptions in Rest Api.


1 Answers

throw new WebApplicationException(Response.status(Status.NOT_FOUND)// Or another Status
                .entity("Error Message").build());
like image 165
cgalleguillosm Avatar answered Oct 27 '22 07:10

cgalleguillosm