Producing HTTP interface description information using spring custom annotations

Source: Internet
Author: User

Recently in a mobile phone background project, using the SPRINGMVC, the development of the interface is the HTTP interface. After the interface is finished, you need to add information such as the name test address of the interface in the webpage to the Web page, so it is very troublesome and easy to leak. A custom annotation was written to add the description of the interface to the interface through annotations, describe the interface information through annotations, and test the address of the production interface.


First look at the method of use and the final effect

@ResponseBody @requestmapping ("/getbuswaiting") @AppInterface (value= "Get waiting Information", group= "test", order=1,params={@ Interfaceparam (name= "Linename", desc= "line name", testvalue= "B2"), @InterfaceParam (name= "Isupdown", desc= "upper and lower line identification", Testvalue= "1"), @InterfaceParam (name= "Stationnum", desc= "station order", testvalue= "0"), @InterfaceParam (name= "Len", desc= "Length" , testvalue= ")}) public Appresponse getbuswaitinginfo (String linename,integer isupdown,integer Stationnum,integer Len) {appresponse result = new Appresponse (); return result;}

The resulting effect


Here is a concrete implementation

Interface Description Class

/**   * <p> Creator: Wang Cheng  </p> * <p> creation time: December 8, 2014 PM 5:28:11  </p> * <p> class Description: Tag mobile app interface, interface description information, for generating interface information and test path </p> * <p> Copyright Note: © tiamaes </p> */@Documented @target ( Elementtype.method) @Retention (retentionpolicy.runtime) public @interface appinterface {/** * <p> method Description: Interface name </ p> * @return String */string value ();/** * <p> Method Description: Group </p> * @return String */string Group () default "";/** * <p> Method Description: Sort </p> * @return int */int order () Default 0;/** * <p> method Description: Parameter list </p> * @return Interface Param[] */interfaceparam[] params () default {};}
Interface parameter class
/**   * <p> Creator: Wang Cheng  </p> * <p> creation time: December 8, 2014 PM 5:29:34  </p> * <p> class Description: Mobile App Interface parameter Description </p> * <p> Copyright Note: © tiamaes </p> */@Documented @target (Elementtype.method) @Retention ( retentionpolicy.runtime) public @interface interfaceparam {/** * <p> method Description: Parameter name </p> * @return String */string Name ();/** * <p> Method Description: Interface Description </p> * @return String */string desc ();/** * <p> method Description: Test parameter value </p> * @return S Tring */string testvalue () Default "";}
TestValue supports custom variables, which are implemented in the main class

Package Com.tiamaes.gjds.app.aop;import Java.util.arraylist;import Java.util.collections;import Java.util.comparator;import Java.util.hashmap;import Java.util.list;import Java.util.map;import Java.util.map.entry;import Org.apache.commons.logging.log;import Org.apache.commons.logging.logfactory;import Org.springframework.beans.beansexception;import Org.springframework.beans.factory.beanfactoryutils;import Org.springframework.beans.factory.initializingbean;import Org.springframework.context.applicationcontext;import Org.springframework.context.applicationcontextaware;import Org.springframework.util.stringutils;import Org.springframework.web.context.webapplicationcontext;import Org.springframework.web.method.HandlerMethod; Import Org.springframework.web.servlet.handlermapping;import Org.springframework.web.servlet.mvc.condition.patternsrequestcondition;import Org.springframework.web.servlet.mvc.method.requestmappinginfo;import Org.springframework.web.servlet.mvc.method.annotation.RequestMaPpinghandlermapping;import Com.tiamaes.gjds.app.annotation.appinterface;import Com.tiamaes.gjds.app.annotation.interfaceparam;import Com.tiamaes.gjds.app.base.requestmethodmapping;import Com.tiamaes.gjds.app.base.requestmethodparameter;import Com.tiamaes.gjds.app.base.variable;import com.tiamaes.gjds.util.setutils;/** * <p> class Description: Generate interface description information and put into application </p> * <p> Creator: Wang Cheng </p> * & Lt;p> created: January 19, 2015 PM 4:42:24 </p> * <p> Copyright Note: ©2015 tiamaes </p> * @see com.tiamaes.gjds.app.annot ation. Appinterface */public class Interfaceannotationconfigprocesser implements applicationcontextaware,initializingbean{ Private Log logger = Logfactory.getlog (GetClass ());p rivate map<string,list<requestmethodmapping>> Mappers = new hashmap<string,list<requestmethodmapping>> ();p rivate Webapplicationcontext applicationcontext;/** * <p> Method Description: Load interface with {@link com.tiamaes.gjds.app.annotation.AppInterface} annotations </p> * <p> first you need to get all the interfaces and then filter methodsWith @appinterface</p> * <p> Creator: Wang Cheng </p> * <p> creation time: January 10, 2015 morning 10:50:06 </p> */public void Loadhandlermapping () {this.logger.info ("initialization Configuration"); Map<string, handlermapping> handlers = Beanfactoryutils.beansoftypeincludingancestors (ApplicationContext, Handlermapping.class, True, false); for (entry<string, handlermapping> entry:handlers.entrySet ()) { handlermapping mapping = Entry.getvalue (); if (mapping instanceof requestmappinghandlermapping) { Requestmappinghandlermapping RequestHandler = (requestmappinghandlermapping) mapping; Map<requestmappinginfo, handlermethod> handlermethods = Requesthandler.gethandlermethods (); for (Entry< Requestmappinginfo, handlermethod> HandlerMethod:handlerMethods.entrySet ()) {Appinterface annotation = Handlermethod.getvalue (). Getmethodannotation (Appinterface.class); if (annotation== null) continue; Patternsrequestcondition patternscondition = Handlermethod.getkey (). Getpatternscondition (); String Requesturl = Setutils.firsT (Patternscondition.getpatterns ()); This.register (Requesturl, Annotation,handlermethod.getvalue (). GetBeanType ()) ;}}}} /** * <p> Method Description: Registration Method </p> * <p> Creator: Wang Cheng </p> * <p> created: January 10, 2015 morning 10:50:06 </p> * @pa Ram Requesturl * @param annotation * @param beantype */private void Register (String requesturl, Appinterface annotation,c Lass<?> beantype) {String group = Annotation.group (); list<requestmethodmapping> groupmappers = This.mappers.get (group), if (groupmappers = = null) Groupmappers = new Arraylist<requestmethodmapping> (); Requestmethodmapping mapper = new requestmethodmapping (); Mapper.setgroup (group); Mapper.setcontroller ( Beantype.getname ()); Mapper.setorder (Annotation.order ()); Mapper.setname (Annotation.value ()); MAPPER.SETURL ( Requesturl); Mapper.setparams (This.toparameters (Annotation.params ())); Groupmappers.add (mapper); this.mappers.put (Group, groupmappers);} /** * <p> Method Description: Read Parameters </p> * <p> Creator: Wang Cheng </p> * <p> created on: 2015 January 10 Morning 10:50:06 </p> * @param params * @return */private list<requestmethodparameter> toparameters ( Interfaceparam[] params) {list<requestmethodparameter> parameters = new Arraylist<requestmethodparameter > (); for (Interfaceparam param:params) {Requestmethodparameter bean = new Requestmethodparameter (); Bean.setname ( Param.name ()); Bean.setdesc (Param.desc ()); if (Stringutils.startswithignorecase (Param.testvalue (), "#")) {String var = Param.testvalue (); String value = getbyvariable (var.substring (Var.indexof ("#") +1)); Bean.settestvalue (value);} Else{bean.settestvalue (Param.testvalue ());} Parameters.Add (bean);} return parameters;} /** * <p> Method Description: Get the value of a variable </p> * <p> Creator: Wang Cheng </p> * <p> created: January 10, 2015 Morning 10:50:06 </p> * @ param var * @return */private string getbyvariable (string var) {Variable Variable = variable.valueof (Var); return Variable. GetValue ();} /** * <p> Method Description: Sort the interface method according to grouping </p> * <p> Creator: Wang Cheng </p> * <p> created: January 201520th pm 16:00:06 </p> */private void Ordermappers () {for (entry<string,list<requestmethodmapping>> Entry:this.mappers.entrySet ()) {Collections.sort () {Entry.getvalue (), new comparator<requestmethodmapping> () {@ overridepublic int Compare (requestmethodmapping O1, requestmethodmapping O2) {Integer one = O1.getorder (); . GetOrder (); if (one! = null && one! = null) return One.intvalue ()-two.intvalue (); return 0;}});}} @Overridepublic void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexception { This.applicationcontext = (webapplicationcontext) ApplicationContext;} @Overridepublic void Afterpropertiesset () throws Exception {this.loadhandlermapping (); This.ordermappers (); This.applicationContext.getServletContext (). SetAttribute ("API", this.mappers);} /** * <p> Method Description: Refresh interface Information </p> * <p> Creator: Wang Cheng </p> * <p> created: January 10, 2015 Morning 10:50:06 </p> * @ Throws Exception */public void Refresh () throws Exception{this.mappers = new hashmap<string,list<requestmethodmapping>> (); This.afterpropertiesset ();}} 
Using C-Label production Interface description information

<div class= "Api-layout" ><table class= "api-table" ><tr class= "Api-table-header" > <td width= "300p x; " > Interface Address </td> <td width= "200px;" > Interface name </td> <td> parameter description </td> </tr><c:foreach items= "${api}" var= "map" > &lt ;tr> <td colspan= "3" class= "Api-group" ><c:out value= "${map.key}"/></td></tr><c:forea CH items= "${map.value}" var= "list" > <tr> <td width= "300px;" > <a href= "<%=baseurl%>${list.url}${list.testurl}" target= "_blank" >${list.url}</a> < /TD> <td width= "200px;" >${list.name}</td> <td><c:foreach items= "${list.params}" var= "params" >${params.name}:${ Params.desc}<br/></c:foreach></td> </tr> </c:foreach></c:foreach></ta Ble></div>







Producing HTTP interface description information using spring custom annotations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.