Interpretation of Java EE filter priority control example
In Java EE applications, we often need to sort and execute multiple filters (Filters).
This can be used@Priority
Annotations to implement.
@Priority
Annotation accepts an integer parameter that specifies the order in which the filter is executed.
The lower the value, the higher the priority.
Below, we will use an instance to analyze in detail how to use it@Priority
Annotations to control the execution order of the filter.
Implement filters
TimeFilter
This filter is used to record the time it takes to execute the resource method. pass@Priority(1)
Annotation, we set its priority to 1.
@Priority(1) @Provider public class TimeFilter implements ContainerRequestFilter, ContainerResponseFilter { @Override public void filter(ContainerRequestContext reqContext) throws IOException { ("-- TimeFilter request --"); ("start-time", ()); } @Override public void filter(ContainerRequestContext reqContext, ContainerResponseContext resContext) throws IOException { ("-- TimeFilter response --"); long startTime = (long) ("start-time"); ("Time taken for request %s: %s milli secs%n", ().getPath(), () - startTime ); } }
LogFilter (LogFilter)
This filter is used to record the header information of requests and responses. pass@Priority(2)
Annotation, we set its priority to 2.
@Priority(2) @Provider public class LogFilter implements ContainerRequestFilter, ContainerResponseFilter { @Override public void filter(ContainerRequestContext reqContext) throws IOException { ("-- req headers --"); log((), ()); } @Override public void filter(ContainerRequestContext reqContext, ContainerResponseContext resContext) throws IOException { ("-- res headers --"); log((), ()); } private void log(UriInfo uriInfo, MultivaluedMap<String, ?> headers) { ("Path: " + ()); ().forEach(h -> (() + ": " + ())); } }
JAX-RS Resources
@Path("/") public class MyResource { @GET @Path("{path:.*}") public String getResponse(@PathParam("path") String path) { ("creating response for '%s'%n", path); return "dummy-response for " + path; } }
Running example
To run these examples, you need to run the project'sConfigure embedded Tomcat and start with the following command:
mvn tomcat7:run-war
Output result
Use the HTTPie tool to access the resource, and the output of the server console is as follows:
-- TimeFilter request --
-- req headers --
Path: customers
host: [localhost:8080]
connection: [keep-alive]
accept-encoding: [gzip, deflate]
accept: [*/*]
user-agent: [HTTPie/0.9.9]
creating response for 'customers'
-- res headers --
Path: customers
Content-Type: [text/plain]
-- TimeFilter response --
Time taken for request customers: 13 milli secs
From the output results, it can be seen thatTimeFilter
existLogFilter
Execute before. If we exchange the priority of these two filters, their execution order will also change accordingly.
Sample Project
Technology stack and dependencies
- jersey-server 2.25.1: Jersey core server implementation.
- jersey-container-servlet 2.25.1: Jersey core Servlet implementation.
- JDK 1.8
- Maven 3.3.9
Through this example, we can see how to use it@Priority
Annotations to control the execution order of filters in Java EE applications.
This is very useful in actual development and can help us better manage and optimize the performance of our application.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.