Coding specifications in the work, which need to be written in the future.
- Before encoding, first write according to Alibaba's development specifications, if using idea, use its plug-ins.
- In the case of springmvc, the following styles are best used to define the path:
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
- In the case of springboot or springMvc, the controller layer is added.Front-end arguments exceed three objects whose param should be defined.Its attribute name needs to be consistent with its corresponding entity class, encapsulating the data using beanutils.Examples are as follows:
Corresponding param class
package com.macro.mall.dto; import com.macro.mall.validator.FlagValidator; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.validation.constraints.Min; import javax.validation.constraints.NotEmpty; /** * Brand Delivery Parameters */ @Data public class PmsBrandParam { @ApiModelProperty(value = "Brand name",required = true) @NotEmpty(message = "Name cannot be empty") private String name; @ApiModelProperty(value = "Brand initials") private String firstLetter; @ApiModelProperty(value = "sort field") @Min(value = 0, message = "Sort Min 0") private Integer sort; @ApiModelProperty(value = "Is it a manufacturer") @FlagValidator(value = {"0","1"}, message = "Incorrect factory status") private Integer factoryStatus; @ApiModelProperty(value = "Whether to display") @FlagValidator(value = {"0","1"}, message = "Incorrect display status") private Integer showStatus; @ApiModelProperty(value = "brand logo",required = true) @NotEmpty(message = "brand logo Cannot be empty") private String logo; @ApiModelProperty(value = "Brand Map") private String bigPic; @ApiModelProperty(value = "Brand Story") private String brandStory; }
Corresponding Entity Classes
package com.macro.mall.model; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.io.Serializable; @Data public class PmsBrand implements Serializable { private Long id; private String name; @ApiModelProperty(value = "Initials") private String firstLetter; private Integer sort; @ApiModelProperty(value = "Is it a brand manufacturer:0->No; 1->yes") private Integer factoryStatus; private Integer showStatus; @ApiModelProperty(value = "Product Quantity") private Integer productCount; @ApiModelProperty(value = "Number of product reviews") private Integer productCommentCount; @ApiModelProperty(value = "brand logo") private String logo; @ApiModelProperty(value = "District Map") private String bigPic; @ApiModelProperty(value = "Brand Story") private String brandStory; private static final long serialVersionUID = 1L; }
Using BeanUtils.copyProperties(pmsBrandParam, pmsBrand); you can complete the encapsulation of the corresponding data.
4. When the controller returns data to the front end.Unified return object types and corresponding enumerations should be defined to make them more uniform.Examples are as follows:
Common Return Objects (modified as required by the project)
package com.macro.mall.common.api; /** * Universal Return Object */ public class CommonResult<T> { private long code; private String message; private T data; protected CommonResult() { } protected CommonResult(long code, String message, T data) { this.code = code; this.message = message; this.data = data; } /** * Return Results Successfully * * @param data Data Obtained */ public static <T> CommonResult<T> success(T data) { return new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data); } /** * Return Results Successfully * * @param data Data Obtained * @param message Prompt message */ public static <T> CommonResult<T> success(T data, String message) { return new CommonResult<T>(ResultCode.SUCCESS.getCode(), message, data); } /** * Failed Return Result * @param errorCode Error Code */ public static <T> CommonResult<T> failed(IErrorCode errorCode) { return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null); } /** * Failed Return Result * @param message Prompt message */ public static <T> CommonResult<T> failed(String message) { return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null); } /** * Failed Return Result */ public static <T> CommonResult<T> failed() { return failed(ResultCode.FAILED); } /** * Parameter validation failure returns results */ public static <T> CommonResult<T> validateFailed() { return failed(ResultCode.VALIDATE_FAILED); } /** * Parameter validation failure returns results * @param message Prompt message */ public static <T> CommonResult<T> validateFailed(String message) { return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null); } /** * Unlogged Return Results */ public static <T> CommonResult<T> unauthorized(T data) { return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data); } /** * Unauthorized Return Results */ public static <T> CommonResult<T> forbidden(T data) { return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data); } public long getCode() { return code; } public void setCode(long code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public T getData() { return data; } public void setData(T data) { this.data = data; } }
enumeration
package com.macro.mall.common.api; /** * Some common API opcodes are enumerated */ public enum ResultCode implements IErrorCode { SUCCESS(200, "Operation Successful"), FAILED(500, "operation failed"), VALIDATE_FAILED(404, "Parameter Check Failed"), UNAUTHORIZED(401, "Not logged in yet or token Has expired"), FORBIDDEN(403, "No related permissions"); private long code; private String message; private ResultCode(long code, String message) { this.code = code; this.message = message; } public long getCode() { return code; } public String getMessage() { return message; } }
- When controller s do log output, they should be configured with AOP to define the object for a uniform log output.You should reduce the use of Logger and set up unified configuration classes on aop, which will make your business logic more unified.Examples are as follows:
weblog entity class
package com.macro.mall.bo; import lombok.Data; /** * Controller Layer's Log Encapsulation Class */ @Data public class WebLog { /** * Pedagogical operation */ private String description; /** * Operating Users */ private String username; /** * Operation time */ private Long startTime; /** * Time consumed */ private Integer spendTime; /** * Root Path */ private String basePath; /** * URI */ private String uri; /** * URL */ private String url; /** * Request Type */ private String method; /** * IP address */ private String ip; private Object parameter; private Object result; }
aop configuration class
package com.macro.mall.component; import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.URLUtil; import cn.hutool.json.JSON; import cn.hutool.json.JSONUtil; import com.macro.mall.bo.WebLog; import io.swagger.annotations.ApiOperation; import net.logstash.logback.marker.Markers; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Unified Log Processing Face */ @Aspect @Component @Order(1) public class WebLogAspect { private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class); @Pointcut("execution(public * com.macro.mall.controller.*.*(..))") public void webLog() { } @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { System.out.println("before========="); } @AfterReturning(value = "webLog()", returning = "ret") public void doAfterReturning(Object ret) throws Throwable { System.out.println("AfterReturning====="); } @Around("webLog()") public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("Around====="); long startTime = System.currentTimeMillis(); //Get the current request object ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //Record request information (passed into Elasticsearch via Logstash) WebLog webLog = new WebLog(); Object result = joinPoint.proceed(); Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod(); if (method.isAnnotationPresent(ApiOperation.class)) { ApiOperation log = method.getAnnotation(ApiOperation.class); webLog.setDescription(log.value()); } long endTime = System.currentTimeMillis(); String urlStr = request.getRequestURL().toString(); webLog.setBasePath(StrUtil.removeSuffix(urlStr, URLUtil.url(urlStr).getPath())); webLog.setIp(request.getRemoteUser()); webLog.setMethod(request.getMethod()); webLog.setParameter(getParameter(method, joinPoint.getArgs())); webLog.setResult(result); webLog.setSpendTime((int) (endTime - startTime)); webLog.setStartTime(startTime); webLog.setUri(request.getRequestURI()); webLog.setUrl(request.getRequestURL().toString()); Map<String,Object> logMap = new HashMap<>(); logMap.put("url",webLog.getUrl()); logMap.put("method",webLog.getMethod()); logMap.put("parameter",webLog.getParameter()); logMap.put("spendTime",webLog.getSpendTime()); logMap.put("description",webLog.getDescription()); // LOGGER.info("{}", JSONUtil.parse(webLog)); LOGGER.info(Markers.appendEntries(logMap), JSONUtil.parse(webLog).toString()); return result; } /** * Get request parameters based on method and parameters passed in */ private Object getParameter(Method method, Object[] args) { List<Object> argList = new ArrayList<>(); Parameter[] parameters = method.getParameters(); for (int i = 0; i < parameters.length; i++) { //Use RequestBody comment-modified parameters as request parameters RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class); if (requestBody != null) { argList.add(args[i]); } //RequestParam annotated parameters as request parameters RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); if (requestParam != null) { Map<String, Object> map = new HashMap<>(); String key = parameters[i].getName(); if (!StringUtils.isEmpty(requestParam.value())) { key = requestParam.value(); } map.put(key, args[i]); argList.add(map); } } if (argList.size() == 0) { return null; } else if (argList.size() == 1) { return argList.get(0); } else { return argList; } } }
xml configuration file
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <!--apply name--> <property name="APP_NAME" value="mall-admin"/> <!--Log file save path--> <property name="LOG_FILE_PATH" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/logs}"/> <contextName>${APP_NAME}</contextName> <!--Log daily to file appender--> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern> <maxHistory>30</maxHistory> </rollingPolicy> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> </appender> <!--Output to logstash Of appender--> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>localhost:4560</destination> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/> </appender> <root level="INFO"> <appender-ref ref="CONSOLE"/> <appender-ref ref="FILE"/> <appender-ref ref="LOGSTASH"/> </root> </configuration>
The above summary is by looking at open source projects mall Shop Later, it is recorded based on the shortcomings that are now being written.The next project guarantees that it is necessary to make changes as required.The above specifications will be updated.Come on.