-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($Starter): add HttpApiScanHelper.java
Scan HTTP API for microservice
- Loading branch information
Johnny Miller (锺俊)
committed
Dec 25, 2020
1 parent
97aadce
commit 629e066
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...-starter/src/main/java/com/jmsoftware/maf/springbootstarter/helper/HttpApiScanHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RequestMappingInfo, HandlerMethod> scan(@NotBlank String includedPackage) { | ||
Map<RequestMappingInfo, HandlerMethod> handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); | ||
log.debug("Scanned request mapping info: {}", handlerMethods); | ||
val filteredHandlerMethods = new LinkedHashMap<RequestMappingInfo, HandlerMethod>(); | ||
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; | ||
} | ||
} |