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

时间:2015-09-01 09:28:06

标签: java rest jersey weblogic jax-rs

我使用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.>

这是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;
}
  }

请求过滤器类

@Provider
public class ReqFilter implements ContainerRequestFilter {

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

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


 }

日志文件显示我调用我的网址时没有写任何内容。如何注册这样一个类?

2 个答案:

答案 0 :(得分: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;
}

第二种方法是使用@Provider。但是如果你这样做,你需要延长Application课程,留空!

结帐this blog entry for a complete example

答案 1 :(得分:1)

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

<library-ref>
  <library-name>jax-rs</library-name>
  <specification-version>2.0</specification-version>
</library-ref>

并且过滤器已注册。但是这搞砸了我所有的Java EE注释,所有从EJB调用中获取数据的URL都被破坏了。