How to write global filter for mobilefirst 8.0 jav

2019-09-09 21:32发布

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 ?

2条回答
混吃等死
2楼-- · 2019-09-09 22:09

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.

查看更多
Deceive 欺骗
3楼-- · 2019-09-09 22:10

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();
    }
}
查看更多
登录 后发表回答