-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 修复FeignClient不支持RequestMapping #205
- Loading branch information
1 parent
c56babe
commit f9782a1
Showing
4 changed files
with
67 additions
and
3 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
50 changes: 50 additions & 0 deletions
50
...rvice-servlet/src/main/kotlin/com/tencent/devops/service/feign/CustomSpringMvcContract.kt
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,50 @@ | ||
package com.tencent.devops.service.feign | ||
|
||
import feign.MethodMetadata | ||
import feign.Util.emptyToNull | ||
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor | ||
import org.springframework.cloud.openfeign.support.SpringMvcContract | ||
import org.springframework.context.ConfigurableApplicationContext | ||
import org.springframework.core.annotation.AnnotatedElementUtils.findMergedAnnotation | ||
import org.springframework.core.convert.ConversionService | ||
import org.springframework.core.io.DefaultResourceLoader | ||
import org.springframework.util.StringUtils | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
|
||
/** | ||
* 解决feign client不允许@RequestMapping注解的问题 | ||
* */ | ||
class CustomSpringMvcContract( | ||
annotatedParameterProcessors: List<AnnotatedParameterProcessor>, | ||
conversionService: ConversionService, | ||
private val decodeSlash: Boolean, | ||
) : SpringMvcContract(annotatedParameterProcessors, conversionService, decodeSlash) { | ||
private val resourceLoader = DefaultResourceLoader() | ||
override fun processAnnotationOnClass(data: MethodMetadata, clz: Class<*>) { | ||
if (clz.interfaces.isEmpty()) { | ||
val classAnnotation = findMergedAnnotation(clz, RequestMapping::class.java) | ||
if (classAnnotation != null) { | ||
// Prepend path from class annotation if specified | ||
if (classAnnotation.value.isNotEmpty()) { | ||
var pathValue = emptyToNull(classAnnotation.value[0]) | ||
pathValue = resolve(pathValue) | ||
if (!pathValue.startsWith("/")) { | ||
pathValue = "/$pathValue" | ||
} | ||
data.template().uri(pathValue) | ||
if (data.template().decodeSlash() != decodeSlash) { | ||
data.template().decodeSlash(decodeSlash) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun resolve(value: String): String { | ||
return if (StringUtils.hasText(value) && resourceLoader is ConfigurableApplicationContext) { | ||
(resourceLoader as ConfigurableApplicationContext).environment.resolvePlaceholders(value) | ||
} else { | ||
value | ||
} | ||
} | ||
} |
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