How to write global filter for mobilefirst 8.0 jav

2019-09-09 21:57发布

问题:

I need a global filter on mobilefirst 8.0 java adapter. Am tring to write ContainerRequestFilter filter. But i need some mobilefirst data in there. ConfigurationApi,AdaptersAPI. How can i get it in this context ? Or there have other way to call some code with all java adapter methods ?

回答1:

You can write a ContainerRequestFilter and use it with an adapter. All you need to do is add to the getClasses() method in the adapter application class (unless it's in the same package as the application class, in this case it will happen automatically).

You can use the @Context annotation in filters to inject any MFP API you need, just like in your resource classes.

Here is a working example:

public class MyRequestFilter implements ContainerRequestFilter {

    @Context
    ConfigurationAPI configApi;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        doStuff();
    }
}


回答2:

Thanks all! Question was solved. Helped this page https://jax-rs-spec.java.net/nonav/2.0-rev-a/apidocs/index.html about @NameBinding annotation. and additional @Produce annotation on Filter class.