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.
1 Answer