Registering a provider programmatically in jersey that implements exceptionmapper

How can I register my provider programmatically in a jersey that implements the Exceptionmapper provided by the jersey API? I do not want to use the @Provider annotation and want to register the provider with ResourceConfig, how can I do this?

For instance:

public class MyProvider implements ExceptionMapper<WebApplicationException> extends ResourceConfig { public MyProvider() { final Resource.Builder resourceBuilder = Resource.builder(); resourceBuilder.path("helloworld"); final ResourceMethod.Builder methodBuilder = resourceBuilder.addMethod("GET"); methodBuilder.produces(MediaType.TEXT_PLAIN_TYPE) .handledBy(new Inflector<ContainerRequestContext, String>() { @Override public String apply(ContainerRequestContext containerRequestContext) { return "Hello World!"; } }); final Resource resource = resourceBuilder.build(); registerResources(resource); } @Override public Response toResponse(WebApplicationException ex) { String trace = Exceptions.getStackTraceAsString(ex); return Response.status(500).entity(trace).type("text/plain").build(); } } 

Is this being done right?

+5
source share
1 answer

I assume that you do not have ResourceConfig , since you do not seem to know how to use it. Firstly, this is not required. If you use it, it should be a separate class. There you can register a card.

 public class AppConfig extends ResourceConfig { public AppConfig() { register(new MyProvider()); } } 

But you are probably using web.xml. In this case, you can register a provider with the following <init-param>

 <servlet> <servlet-name>MyApplication</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> org.foo.providers.MyProvider </param-value> </init-param> </servlet> 

See “Deploying Applications and Runtimes” for more information on the various deployment models. There are several ways to deploy applications. You can even mix and match (web.xml and ResourceConfig).

+7
source

All Articles