In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the implementation method of printing interface list of springboot startup project". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
Springboot startup project print interface list
Environment
Modify the configuration file
Unified printing of input parameters for Springboot project add interface
Create a new annotation to enhance the parameter printing function
Custom serialization rules
Write parameter print enhancement, here choose surround enhancement
Springboot startup project print interface list environment
Springboot 2.3.2.RELEASE
Modify the configuration file logging: level: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping: trace
Results:
Unified printing of input parameters for Springboot project add interface
Requirements: print the called method name and input parameters when the API is called, and select fastjson when the parameters are formatted
Note: desensitization when using fastjson serialization. It is recommended to uniformly use custom object types as input parameters.
If you do not need parameter desensitization, just use the relevant code in the enhancement and remove the code related to parameter desensitization.
Create a new annotation to enhance @ Target ({ElementType.METHOD}) @ Retention (RetentionPolicy.RUNTIME) @ Documentedpublic @ interface ParamInfo {/ * cancel unified printing parameters * default to false unified printing * if you need to customize parameter printing, assign it to true * / boolean unPrint () default false / * fields that need to be desensitized, such as password, * / String [] fields () default {};} Custom serialization rules / * Serialization filter: value replacement * * / public class ReplaceFieldFilter implements ValueFilter {/ * property name and replacement value to be replaced * key: attribute name * value: replacement value * / private Map fieldMap Public ReplaceFieldFilter () {} public ReplaceFieldFilter (Map fieldMap) {this.fieldMap = fieldMap;} @ Override public Object process (Object o, String name, Object value) {if (! CollectionUtils.isEmpty (fieldMap)) {Iterator iterator = fieldMap.entrySet () .iterator (); while (iterator.hasNext ()) {Map.Entry next = iterator.next () If (next.getKey (). EqualsIgnoreCase (name)) {return next.getValue ();} return value;} public Map getFieldMap () {return fieldMap;} public void setFieldMap (Map fieldMap) {this.fieldMap = fieldMap } / * pass in the field name to be desensitized, and serialize it into * number * / public ReplaceFieldFilter (String...) Fields) {String str = "*"; fieldMap = new HashMap (4); for (String field: fields) {fieldMap.put (field, str) } write parameters print enhancements. Choose Enhancement @ Component@Aspect// to indicate the order in which enhancements are performed. If there are multiple enhancements, the smaller ones are first executed @ Order (0) public class ParamInfoAspect {private static final Logger LOGGER = LoggerFactory.getLogger (ParamInfoAspect.class); @ Around ("execution (* com.service.impl.*.* (..)")) Public Object printParam (ProceedingJoinPoint joinPoint) throws Throwable {long startTime = System.currentTimeMillis (); String requestId = RandomStringUtils.randomAlphanumeric (16); Object returnValue = null; try {Object [] args = joinPoint.getArgs (); / / get method object MethodSignature signature = (MethodSignature) joinPoint.getSignature (); Method method = signature.getMethod () / / obtain desensitization field through annotations, and then initialize fieldMap to complete field desensitization ParamInfo annotation = method.getAnnotation (ParamInfo.class); Map fieldMap = new HashMap (4); fieldMap.put ("password", "*") If (annotation! = null) {/ / get the field name array String [] fields = annotation.fields (); for (String field: fields) {fieldMap.put (field, "*");}} String param / / Parameter integration, multi-field input parameters are integrated into objects, and the input parameter format of a single object remains unchanged if (args.length > 1 | (args.length = = 1 & & args [0] .getClass () = = String.class)) {Map paramMap = new LinkedHashMap (); String [] parameterNames = signature.getParameterNames (); for (int I = 0; I < parameterNames.length) ParamMap.put (parameterNames [I], args [I]);} param = "[" + JSON.toJSONString (paramMap, new ReplaceFieldFilter (fieldMap)) + "]";} else {param = JSON.toJSONString (args, new ReplaceFieldFilter (fieldMap));} String methodName = method.getName () LOGGER.info ("method: [{}], parameter: {}, requestId: [{}]", methodName, param, requestId); returnValue = joinPoint.proceed (); return returnValue;} catch (Exception e) {LOGGER.error ("system is error:", e); / / you can define the error return value returnValue = ErrorCode.SYSTEM_ERROR when a program exception occurs here. Return returnValue;} finally {LOGGER.info ("request cost: {} ms, requestId: [{}]", System.currentTimeMillis ()-startTime, requestId); LOGGER.info ("returnValue: [{}], requestId: [{}]", returnValue, requestId);}} "implementation method of springboot startup project print interface list". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.