简体   繁体   English

以编程方式在jersey中注册提供程序,实现exceptionmapper

[英]Registering a provider programmatically in jersey which implements exceptionmapper

How do I register my provider programmatically in jersey which implements the Exceptionmapper provided by jersey API? 如何以编程方式在jersey中注册我的提供程序,该程序实现了jersey API提供的Exceptionmapper? I don't want to use @Provider annotation and want to register the provider using ResourceConfig, how can I do that? 我不想使用@Provider注释并希望使用ResourceConfig注册提供程序,我该怎么做?

For example: 例如:

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 the correct way to do this? 这是正确的方法吗?

I'm guessing you don't have a ResourceConfig , since you seem to not be sure how to use it. 我猜你没有ResourceConfig ,因为你似乎不确定如何使用它。 For one, it is not required. 首先,它不是必需的。 If you do use it, it should be it's own separate class. 如果你确实使用它,它应该是它自己独立的类。 There you can register the mapper. 在那里你可以注册映射器。

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

But you are probably using a web.xml. 但是你可能正在使用web.xml。 In which case, you can register the provider, with the following <init-param> 在这种情况下,您可以使用以下<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>

Have a look at Application Deployment and Runtime Environments for more information on different deployment models. 有关不同部署模型的更多信息,请查看应用程序部署和运行时环境 There are a few different ways to deploy applications. 部署应用程序有几种不同的方法。 You can even mix and match (web.xml and ResourceConfig). 你甚至可以混合搭配(web.xml和ResourceConfig)。

While @paul-samsotha's answer is correct, still there is implementation trick. 虽然@ paul-samsotha的答案是正确的,但仍有实施技巧。 I want to share it and hope it will help someone. 我想分享它并希望它会帮助某人。

a) Implement your mapper: a)实现你的映射器:

public class MyExceptionMapper implements ExceptionMapper<Throwable>, ResponseErrorMapper {
    ...

b) make sure you declare generic type, otherwise your mapper will never be called b)确保声明泛型类型,否则永远不会调用映射器

public class MyExceptionMapper implements ExceptionMapper/* no generic declaration */, ResponseErrorMapper {
    ...

and may trigger 并可能触发

javax.ws.rs.ProcessingException: Could not find exception type for given ExceptionMapper class: class com...MyExceptionMapper.

c) Register it as resource: c)将其注册为资源:

ResourceConfig config = new ResourceConfig();
config.register(new MyExceptionMapper());

or 要么

config.register(MyExceptionMapper.class);

d) make sure you enforce processing errors as well: d)确保您也强制执行处理错误:

config.setProperties(new LinkedHashMap<String, Object>() {{
    put(org.glassfish.jersey.server.ServerProperties.PROCESSING_RESPONSE_ERRORS_ENABLED, true);
}});

If you're using Spring and want to register the providers programmatically based on the presence of @Path and @Provider annotation you can use the following technique 如果您正在使用Spring并希望根据@Path@Provider注释的存在以编程方式注册提供程序,则可以使用以下技术

@Component
public class JerseyConfig extends ResourceConfig {

  @Autowired
  private ApplicationContext applicationContext;

  @PostConstruct
  public init() {

    applicationContext.getBeansWithAnnotation(Path.class).values().forEach(
      component -> register(component.getClass())
    );
    applicationContext.getBeansWithAnnotation(Provider.class).values().forEach(
      this::register
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM