Jax-RS not registering resource with @provider ann

2019-07-04 09:19发布

I have a rest application running in weblogic 12c using jersey api. I have a http request filter class with an annotation @provider. however when deploying the application the filter class is not registered with the other resource classes that I have added in my ApplicationConfig class. and subsequently I am not able to filter the http requests. below are my logs and the code

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.>

here is the applicationconfig class

@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;
}
  }

request filter class

@Provider
public class ReqFilter implements ContainerRequestFilter {

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

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


 }

the log file shows nothing is written when I call my urls. how does one register such a class?

2条回答
手持菜刀,她持情操
2楼-- · 2019-07-04 09:47

I deployed a shared library weblogic-jax-rs and then referenced it in my weblogic.xml

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

and the filters got registered. however this messed up all my java EE annotations and all urls getting data from EJB invocations are broken.

查看更多
劫难
3楼-- · 2019-07-04 09:51

There are 2 approaches. The one you described must work, however, try to put the Filter first in the list of classes.

@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;
}

The second approach is to use @Provider. But if you do so, you need to extend the Application class and leave it empty!

Checkout this blog entry for a complete example

查看更多
登录 后发表回答