From 629e06671be275bb7c5e47739fb8efa58d67a35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Miller=20=28=E9=94=BA=E4=BF=8A=29?= Date: Fri, 25 Dec 2020 16:07:02 +0800 Subject: [PATCH] feat($Starter): add HttpApiScanHelper.java Scan HTTP API for microservice --- .../controller/PermissionController.java | 6 ++ .../aspect/ExceptionControllerAdvice.java | 2 +- .../configuration/MafAutoConfiguration.java | 8 +++ .../helper/HttpApiScanHelper.java | 59 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/helper/HttpApiScanHelper.java diff --git a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java index 4c7779ec..1e6084d2 100644 --- a/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java +++ b/auth-center/src/main/java/com/jmsoftware/maf/authcenter/permission/controller/PermissionController.java @@ -1,7 +1,9 @@ package com.jmsoftware.maf.authcenter.permission.controller; import com.jmsoftware.maf.authcenter.permission.service.PermissionService; +import com.jmsoftware.maf.authcenter.universal.configuration.ProjectProperty; import com.jmsoftware.maf.common.bean.ResponseBodyBean; +import com.jmsoftware.maf.springbootstarter.helper.HttpApiScanHelper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; @@ -29,6 +31,8 @@ public class PermissionController { private final PermissionService permissionService; private final DiscoveryClient discoveryClient; + private final HttpApiScanHelper httpApiScanHelper; + private final ProjectProperty projectProperty; @GetMapping("/permissions/services-info") @ApiOperation(value = "Get services info", notes = "Get services info") @@ -41,6 +45,8 @@ public ResponseBodyBean servicesInfo() { log.info("Instances: {}", instances); resultMap.put(service, instances); }); + val httpApiMap = httpApiScanHelper.scan(projectProperty.getBasePackage()); + resultMap.put("httpApiMap", httpApiMap.toString()); return ResponseBodyBean.ofSuccess(resultMap); } } diff --git a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/aspect/ExceptionControllerAdvice.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/aspect/ExceptionControllerAdvice.java index 91bc9fbf..94bd0e0e 100644 --- a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/aspect/ExceptionControllerAdvice.java +++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/aspect/ExceptionControllerAdvice.java @@ -131,7 +131,7 @@ public ResponseBodyBean handleException(HttpServletRequest request, HttpServl return ResponseBodyBean.ofStatus(HttpStatus.FORBIDDEN.value(), removeLineSeparator(exception.getMessage()), null); } - log.error("Internal system exception occurred! Exception message: {} ", exception.getMessage(), exception); + log.error("Internal server exception occurred! Exception message: {} ", exception.getMessage(), exception); response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); return ResponseBodyBean.ofStatus(HttpStatus.INTERNAL_SERVER_ERROR, "Exception message: " + removeLineSeparator(exception.getMessage())); diff --git a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/MafAutoConfiguration.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/MafAutoConfiguration.java index d306ea8b..a68fcab7 100644 --- a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/MafAutoConfiguration.java +++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/configuration/MafAutoConfiguration.java @@ -5,6 +5,7 @@ import com.jmsoftware.maf.springbootstarter.controller.GlobalErrorController; import com.jmsoftware.maf.springbootstarter.controller.RedirectController; import com.jmsoftware.maf.springbootstarter.filter.AccessLogFilter; +import com.jmsoftware.maf.springbootstarter.helper.HttpApiScanHelper; import com.jmsoftware.maf.springbootstarter.helper.IpHelper; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.AutoConfigureOrder; @@ -20,6 +21,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.annotation.PostConstruct; import java.util.List; @@ -99,4 +101,10 @@ public GlobalErrorController globalErrorController(ErrorAttributes errorAttribut log.warn("Initial bean: {}", GlobalErrorController.class.getName()); return new GlobalErrorController(errorAttributes, serverProperties, errorViewResolvers); } + + @Bean + public HttpApiScanHelper httpApiScanHelper(RequestMappingHandlerMapping requestMappingHandlerMapping) { + log.warn("Initial bean: {}", HttpApiScanHelper.class.getName()); + return new HttpApiScanHelper(requestMappingHandlerMapping); + } } diff --git a/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/helper/HttpApiScanHelper.java b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/helper/HttpApiScanHelper.java new file mode 100644 index 00000000..f7aec220 --- /dev/null +++ b/spring-boot-starter/src/main/java/com/jmsoftware/maf/springbootstarter/helper/HttpApiScanHelper.java @@ -0,0 +1,59 @@ +package com.jmsoftware.maf.springbootstarter.helper; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.mvc.method.RequestMappingInfo; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; + +import javax.validation.constraints.NotBlank; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Description: HttpApiScanHelper, scanning HTTP API for microservice. + * + * @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 12/25/2020 2:43 PM + **/ +@Slf4j +@Validated +@RequiredArgsConstructor +public class HttpApiScanHelper { + private final RequestMappingHandlerMapping requestMappingHandlerMapping; + private static final String EXCLUDED_PACKAGE = "com.jmsoftware.maf.springbootstarter"; + + /** + * Scan map. + * + * @param includedPackage the included package + * @return the map + * @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 12/25/2020 4:02 PM + */ + public Map scan(@NotBlank String includedPackage) { + Map handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); + log.debug("Scanned request mapping info: {}", handlerMethods); + val filteredHandlerMethods = new LinkedHashMap(); + handlerMethods.forEach((requestMappingInfo, handlerMethod) -> { + if (handlerMethod.toString().contains(includedPackage) && + !handlerMethod.toString().contains(EXCLUDED_PACKAGE)) { + try { + RequestMethod requestMethod = new ArrayList<>( + requestMappingInfo.getMethodsCondition().getMethods()).get(0); + log.debug("Request: [{}] {}, handler method: {}", requestMethod, + requestMappingInfo.getPatternsCondition(), handlerMethod); + filteredHandlerMethods.put(requestMappingInfo, handlerMethod); + } catch (Exception e) { + log.warn( + "Exception occurred when getting request method. Exception message: {}. Request mapping " + + "info: {}", + e.getMessage(), requestMappingInfo); + } + } + }); + return filteredHandlerMethods; + } +}