Jax-RS没有使用@provider注释注册资源

mar*_*ani 4 java rest weblogic jax-rs jersey

我使用jersey api在weblogic 12c中运行了一个rest应用程序.我有一个带有注释@provider的http请求过滤器类.但是,在部署应用程序时,过滤器类未向我在ApplicationConfig类中添加的其他资源类注册.然后我无法过滤http请求.下面是我的日志和代码

Sep 01, 2015 11:16:12 AM weblogic.jaxrs.server.portable.servlet.JerseyServletContainerInitializer onStartup
INFO: Number of JAX-RS specific classes to be examined:24
Sep 01, 2015 11:16:12 AM weblogic.jaxrs.server.portable.servlet.BaseServletContainerInitializer addServletWithApplication
INFO: Registering the Jersey servlet application, named com.ws.rest.ApplicationConfig, at the servlet mapping, /rest/*, with the Application class of the same name
Sep 01, 2015 11:16:12 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.18.1 02/19/2014 03:28 AM'
Sep 01, 2015 11:16:12 AM com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder <init>
INFO: Instantiated the Application class com.ws.rest.ApplicationConfig
Sep 01, 2015 11:16:14 AM weblogic.jaxrs.onwls.deploy.ejb.provider.EJBComponentProviderFactory getComponentProvider
INFO: Binding the EJB class com.ws.rest.UserController to EJBManagedComponentProvider
Sep 01, 2015 11:16:14 AM weblogic.jaxrs.onwls.deploy.ejb.provider.EJBComponentProviderFactory getComponentProvider
INFO: Binding the EJB class com.ws.rest.ReportsController to EJBManagedComponentProvider
Sep 01, 2015 11:16:14 AM weblogic.jaxrs.onwls.deploy.ejb.provider.EJBComponentProviderFactory getComponentProvider
INFO: Binding the EJB class com.ws.rest.MainController to EJBManagedComponentProvider
Sep 01, 2015 11:16:14 AM weblogic.jaxrs.onwls.deploy.ejb.provider.EJBComponentProviderFactory getComponentProvider
INFO: Binding the EJB class com.ws.rest.BusinessController to EJBManagedComponentProvider
Sep 01, 2015 11:16:14 AM com.sun.jersey.spi.inject.Errors processErrorMessages
WARNING: The following warnings have been detected with resource and/or provider classes:
  WARNING: A HTTP GET method, public void com.ws.rest.BusinessController.resetCalls(), MUST return a non-void type.
<Sep 1, 2015 11:16:14 AM EAT> <Warning> <com.sun.jersey.spi.inject.Errors> <BEA-000000> <The following warnings have been detected with resource and/or provider classes:
  WARNING: A HTTP GET method, public void com.ws.rest.BusinessController.resetCalls(), MUST return a non-void type.> 
<Sep 1, 2015 11:16:14 AM EAT> <Notice> <Log Management> <BEA-170027> <The server has successfully established a connection with the Domain level Diagnostic Service.> 
<Sep 1, 2015 11:16:14 AM EAT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to ADMIN.> 
<Sep 1, 2015 11:16:14 AM EAT> <Notice> <WebLogicServer> <BEA-000365> <Server state changed to RUNNING.>
Run Code Online (Sandbox Code Playgroud)

这是applicationconfig类

@ApplicationPath("/api")

public class ApplicationConfig extends Application{

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> s = new HashSet<Class<?>>();
    s.add(MainController.class);
    s.add(UserController.class);
    s.add(ReqFilter.class);
    s.add(BusinessController.class);
    s.add(ReportsController.class);
    return s;
}

@Override
public Set<Object> getSingletons() {
    MOXyJsonProvider moxyJsonProvider = new MOXyJsonProvider();

    //moxyJsonProvider.setAttributePrefix("@");
    moxyJsonProvider.setFormattedOutput(true);
    moxyJsonProvider.setIncludeRoot(false);
    moxyJsonProvider.setMarshalEmptyCollections(true);

    HashSet<Object> set = new HashSet<Object>(1);
    set.add(moxyJsonProvider);
    return set;
}
  }
Run Code Online (Sandbox Code Playgroud)

请求过滤器类

@Provider
public class ReqFilter implements ContainerRequestFilter {

Logger logger = LoggerFactory.getLogger(ReqFilter.class);

@Override
public void filter(ContainerRequestContext requestContext) throws WebApplicationException {
    logger.info(requestContext.getMethod());
}


 }
Run Code Online (Sandbox Code Playgroud)

日志文件显示我调用我的网址时没有写任何内容.如何注册这样的课程?

ACV*_*ACV 5

有两种方法.您描述的那个必须工作,但是,尝试将Filter放在类列表中的第一位.

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> s = new HashSet<Class<?>>();
    s.add(ReqFilter.class);
    s.add(MainController.class);
    s.add(UserController.class);
    s.add(BusinessController.class);
    s.add(ReportsController.class);
    return s;
}
Run Code Online (Sandbox Code Playgroud)

第二种方法是使用@Provider.但是如果你这样做,你需要扩展Application课程并将其留空!

查看此博客条目以获取完整示例


mar*_*ani 1

我部署了一个共享库weblogic-jax-rs ,然后在我的 weblogic.xml 中引用它

<library-ref>
  <library-name>jax-rs</library-name>
  <specification-version>2.0</specification-version>
</library-ref>
Run Code Online (Sandbox Code Playgroud)

并且过滤器已注册。然而,这搞乱了我所有的 java EE 注释,并且所有从 EJB 调用获取数据的 url 都被破坏了。