Recently, a buddy in the group wrote a handleradapter that automatically gets the cookie inside the HTTP request and assembles it into a model to be used directly. Feel very good. So I made a, special to share.
Principle:
Use Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.
Inside the Dispatcherservlet defines:
Private list
For servlet initialization, find the configured handleradapter. Call the Doservice layer layer at the time the servlet request arrivesProtected Handleradapter Gethandleradapter (Object handler) throws Servletexception {for (Handleradapter ha: This.handleradapters) {if (logger.istraceenabled ()) {Logger.trace ("Testing handler adapter [" + Ha + "]");} if (Ha.supports (handler)) {return ha;}} throw new Servletexception ("No Adapter for Handler [" + Handler + "]: Does your handler implement a supported interface lik E Controller? ");}
Let them deal with handler. A implementation code is affixed here:Demo.java:
Package Com.hp.share.annocation;public class Demo { private int id; private String name; private byte sex; public int getId () { return ID; } public void setId (int id) { this.id = ID; } Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public byte Getsex () { return sex; } public void Setsex (Byte sex) { this.sex = sex; } Public Demo () { this.id = 1001; this.name = "Hello World"; This.sex = 1; } @Override public String toString () { return "Demo [id=" + ID + ", name=" + name + ", sex=" + Sex + "]"; }}
Demoannotation.java:
Package Com.hp.share.annocation;import Java.lang.annotation.elementtype;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Retention ( Retentionpolicy.runtime) @Target ({elementtype.parameter}) public @interface demoannotation {}
Demoprocessor.java:
Package Com.hp.share.handler;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import Org.springframework.core.methodparameter;import org.springframework.web.bind.support.WebDataBinderFactory; Import Org.springframework.web.context.request.nativewebrequest;import Org.springframework.web.method.support.handlermethodargumentresolver;import Org.springframework.web.method.support.modelandviewcontainer;import Com.hp.share.annocation.demo;import Com.hp.share.annocation.demoannotation;public class Demoprocessor implements Handlermethodargumentresolver {privat e static Logger Logger = Loggerfactory.getlogger (Demoprocessor.class); public boolean supportsparameter (Methodparameter parameter) {Boolean res = Parameter.getparameterannotation (demoan NOTATION.CLASS)! = NULL; Logger.info ("Start checksum: {}", res); return res; } public Object resolveargument (methodparameter parameter, Modelandviewcontainer Mavcontainer, nativewebrequ EST webRequest, WebdatabindErfactory binderfactory) throws Exception {Logger.info ("This is a sign of annotation execution ~ ~ ~"); return new Demo (); }}
Democontroller.java:
Package Com.hp.share.controller;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.responsebody;import Com.google.common.base.preconditions;import Com.hp.share.annocation.demo;import com.hp.share.annocation.DemoAnnotation; @Controllerpublic class Democontroller { @RequestMapping ("/hello") @ResponseBody public String Demo (@DemoAnnotation demo req) { Preconditions.checknotnull (req); System.out.println (string.valueof (req)); Return "Success"; }}
Configuration Required:<beanclass= "Org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >< Property Name= "Customargumentresolvers" ><list><bean class= "Com.hp.share.handler.DemoProcessor"/> </list></property></bean><mvc:annotation-driven/>
Remember here<mvc:annotation-driven/>And it has to be underneath all the beans. If you configure Annotationmethodhandleradapter, you should put it on the last side of the configuration.Specific reasons to view the spring source (with Dispatcherservlet)
Debug Dispatcherservlet If you find your code does not make a test. Most likely because of a "configuration error or a Handleradapter reference error (or misconfiguration)
In this way, you can easily get to the content of your own custom annotations. If you want to annotate the method, you can still use this class, details to view the source code.
Spring custom annotations Enable controller to get the desired data