简体   繁体   English

ExceptionMapper提供程序不适用于球衣

[英]ExceptionMapper provider not working with jersey

I'm doing a Webservice in java using Jersey. 我正在使用Jersey使用Java进行Web服务。 my dependencies for jersey are: 我对球衣的依赖是:

/* JAX-RS Impl (Jersey) */
compile 'org.glassfish.jersey.inject:jersey-hk2:2.27'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.27'
compile 'org.glassfish.jersey.media:jersey-media-sse:2.27'
compile 'org.glassfish.jersey.core:jersey-server:2.27'

//Jackson implementation 
compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.27'

// Hateoas
compile 'org.glassfish.jersey.ext:jersey-declarative-linking:2.27'

//Jersey Impl test
compile 'org.glassfish.jersey.test-framework:jersey-test-framework-core:2.27'

compileOnly 'javax.servlet:javax.servlet-api:4.0.1'

My class Exception looks like the next snippet, I'm catching all Throwable to test this works. 我的类Exception看起来像下一个代码片段,我正在捕获所有Throwable来测试这项工作。

package com.apporelbotna.appuestas.rest.exception;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class WebserviceExceptionMapper implements ExceptionMapper<Throwable>
{
    public WebserviceExceptionMapper()
    {
        System.out.println("I'm getting created!!! - WebserviceExceptionMapper");
    }

    public Response toResponse(Throwable ex)
    {
        return Response.status(404).build();
    }
}

And my rest Endpoint looks like: 我其余的Endpoint看起来像:

@GET
public Response getUsers() throws Exception
{
    throw new Exception("Hi i'm crashing!");
}

I read the documentation of Jersey and i undestand that i can register the exception mappers using @provider in the class. 我阅读了Jersey的文档,但我无法理解可以使用该类中的@provider注册异常映射器。 But the application doesn't recognize the class. 但是应用程序无法识别该类。 I tried to register my class in my ResourceConfig class. 我试图在ResourceConfig类中注册我的类。 And this it works. 并且它有效。

@Provider
@javax.ws.rs.ApplicationPath("webservice")
public class RestResourceConfig extends ResourceConfig
{
    @Context
    public void setServletContext(ServletContext context)
    {
        if (context != null)
        {
            context.getInitParameter("system.info.allow");
        }
    }

    public RestResourceConfig()
    {
        // Register components in a package
        register(WebserviceExceptionMapper.class);
        packages("com.apporelbotna.appuestas.rest.endpoint");
    }
}

What i'm missing? 我想念的是什么?

packages("com.apporelbotna.appuestas.rest.endpoint") This tells Jersey which package(s) to scan for @Provider and @Path classes and register them. packages("com.apporelbotna.appuestas.rest.endpoint")告诉Jersey哪些软件包扫描@Provider@Path类并进行注册。 You can list multiple packages or you can just use a base package like com.apporelbotna.appuestas and Jersey will scan the package recursively. 您可以列出多个软件包,也可以只使用com.apporelbotna.appuestas类的基本软件包,Jersey将递归扫描该软件包。

So if you want your ExceptionMapper to be registered automatically, do one of the following 因此,如果您希望自动注册ExceptionMapper ,请执行以下一项操作

packages("com.apporelbotna.appuestas.rest.endpoint",
         "com.apporelbotna.appuestas.rest.exception");
// -or-
packages("com.apporelbotna.appuestas.rest");

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

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