Invalid Jersey ExceptionMapper Instance

I am trying to call this exception handler to return a 404 not found answer, but it continues to return 500 internal errors. The Jersey version is 2.22.1. The following is a snippet of code. Appreciate all the help.

Thanks.

Exception mapping class.

package org.learn.rest.messengerdemo.exception; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>{ @Override public Response toResponse(DataNotFoundException ex) { return Response.status(Response.Status.FORBIDDEN).build(); } } 

Exception class

package org.learn.rest.messengerdemo.exception;

 public class DataNotFoundException extends RuntimeException{ private static final long serialVersionUID = 2176642539344388961L; public DataNotFoundException(String message) { super(message); } } 

The class of service method that returns.

 public Message getMessage(long messageId) { Message message = messages.get(messageId); if(message == null) { throw new DataNotFoundException("Message with id " + messageId + " not found"); } return message; } 

And the resource class.

 @GET @Path("/{messageId}") public Message getMessage(@PathParam("messageId") long messageId) { return messageService.getMessage(messageId); } 
+5
source share
3 answers

Looking at web.xml from your previous question , you have this

 <servlet> <servlet-name>Jersey Web Application</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>org.learn.rest.messengerdemo.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 

What this init-parm jersey.config.server.provider.packages jersey.config.server.provider.packages is that Jersey must scan the named package for the @Path annotated resource class and @Provider annotated provider classes and register them.

You only have the resource package org.learn.rest.messengerdemo.resources , but you ExceptionMapper are in another package. The default behavior is to scan recursively, which also means subpackages. Therefore, if you specified org.learn.rest.messengerdemo instead, you would fall into the resource bundle and the exception bundle. Or you can list both packages separated by a comma or semicolon. It will work anyway

 <param-value>org.learn.rest.messengerdemo</param-value> <!-- OR --> <param-value> org.learn.rest.messengerdemo.resources, org.learn.rest.messengerdemo.exception </param-value> 
+14
source

Please note that this is not an annotation issue. I saw people reading comments that, due to the @Provider annotation, did not register it, if you imported the correct provider, it will work. Please find my solution below.

I ran into the same problem when developing a sample REST API. When creating the REST API, I gave the name of the base package, for example org.manish.rest.message , I have to create all the other packages under the base package, like this

  • model - org.manish.rest.message.model
  • - org.manish.rest.message.database
  • resource - org.manish.rest.message.resource

in web.xml init param was specified as

  <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>org.manish.rest.message</param-value> </init-param> 

This means that I registered the basic package in web.xml, some package that I will create under this; JAX-RS will be reviewed based on my requirement and requirement. But when I created my exception package in error, I put the package name org.manish.rest.exception. Since it was not registered in web.xml, so my complete exception class was not considered JAX-RS exception handling. As a correction, I just changed the name of the exception package from org.manish.rest.exception to org.manish.rest.message.exception

After that I did it once in the post man and got the expected result.

Hope this can resolve your request.

Thanks Manish

+1
source

I would like to suggest another option ... modify the mapper package to match the resource package, where you could usually throw an exception.

Assuming the resource class package:

 package org.learn.rest.messengerdemo.resources; 

change the package of the mapping class:

 package org.learn.rest.messengerdemo.exception; 

in

 package org.learn.rest.messengerdemo.resources; 

Thus, you will not deal with files generated by frames or unusual syntax.

I still do not understand why try / catch is not used, but I am restoring my knowledge of Java, so I will not enter any opinion or request any. If his current practice, then I will find out soon enough. And the mappers are an interesting opportunity.

0
source

Source: https://habr.com/ru/post/1234678/


All Articles