-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from yinjihuan/encrypt1.2.2
Encrypt1.2.2
- Loading branch information
Showing
9 changed files
with
204 additions
and
86 deletions.
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
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
74 changes: 74 additions & 0 deletions
74
monkey-api-encrypt-core/src/main/java/com/cxytiandi/encrypt/util/RequestUriUtils.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,74 @@ | ||
package com.cxytiandi.encrypt.util; | ||
|
||
|
||
import com.cxytiandi.encrypt.springboot.HttpMethodTypePrefixConstant; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
public class RequestUriUtils { | ||
|
||
private static String separator = "/"; | ||
|
||
public static String getApiUri(Class<?> clz, Method method, String contextPath) { | ||
String methodType = ""; | ||
StringBuilder uri = new StringBuilder(); | ||
|
||
RequestMapping reqMapping = AnnotationUtils.findAnnotation(clz, RequestMapping.class); | ||
if (reqMapping != null && reqMapping.value() != null && reqMapping.value().length > 0) { | ||
uri.append(formatUri(reqMapping.value()[0])); | ||
} | ||
|
||
|
||
GetMapping getMapping = AnnotationUtils.findAnnotation(method, GetMapping.class); | ||
PostMapping postMapping = AnnotationUtils.findAnnotation(method, PostMapping.class); | ||
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class); | ||
PutMapping putMapping = AnnotationUtils.findAnnotation(method, PutMapping.class); | ||
DeleteMapping deleteMapping = AnnotationUtils.findAnnotation(method, DeleteMapping.class); | ||
|
||
if (getMapping != null && getMapping.value() != null && getMapping.value().length > 0) { | ||
methodType = HttpMethodTypePrefixConstant.GET; | ||
uri.append(formatUri(getMapping.value()[0])); | ||
|
||
} else if (postMapping != null && postMapping.value() != null && postMapping.value().length > 0) { | ||
methodType = HttpMethodTypePrefixConstant.POST; | ||
uri.append(formatUri(postMapping.value()[0])); | ||
|
||
} else if (putMapping != null && putMapping.value() != null && putMapping.value().length > 0) { | ||
methodType = HttpMethodTypePrefixConstant.PUT; | ||
uri.append(formatUri(putMapping.value()[0])); | ||
|
||
} else if (deleteMapping != null && deleteMapping.value() != null && deleteMapping.value().length > 0) { | ||
methodType = HttpMethodTypePrefixConstant.DELETE; | ||
uri.append(formatUri(deleteMapping.value()[0])); | ||
|
||
} else if (requestMapping != null && requestMapping.value() != null && requestMapping.value().length > 0) { | ||
RequestMethod requestMethod = RequestMethod.GET; | ||
if (requestMapping.method().length > 0) { | ||
requestMethod = requestMapping.method()[0]; | ||
} | ||
|
||
methodType = requestMethod.name().toLowerCase() + ":"; | ||
uri.append(formatUri(requestMapping.value()[0])); | ||
|
||
} | ||
|
||
if (StringUtils.hasText(contextPath) && !separator.equals(contextPath)) { | ||
if (contextPath.endsWith(separator)) { | ||
contextPath = contextPath.substring(0, contextPath.length() - 1); | ||
} | ||
return methodType + contextPath + uri.toString(); | ||
} | ||
|
||
return methodType + uri.toString(); | ||
} | ||
|
||
private static String formatUri(String uri) { | ||
if (uri.startsWith(separator)) { | ||
return uri; | ||
} | ||
return separator + uri; | ||
} | ||
} |
Oops, something went wrong.