Skip to content

Commit

Permalink
feat: 修复FeignClient不支持RequestMapping #205
Browse files Browse the repository at this point in the history
  • Loading branch information
felixncheng committed Jul 16, 2024
1 parent c56babe commit f9782a1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.tencent.devops.service

import com.tencent.devops.service.feign.CustomSpringMvcContract
import com.tencent.devops.service.feign.FeignFilterRequestMappingHandlerMapping
import feign.Contract
import org.springframework.boot.autoconfigure.AutoConfigureBefore
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor
import org.springframework.cloud.openfeign.FeignClientProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.PropertySource
import org.springframework.core.convert.ConversionService

/**
* Service自动化配置类
Expand All @@ -24,4 +29,14 @@ class ServiceServletAutoConfiguration {
override fun getRequestMappingHandlerMapping() = FeignFilterRequestMappingHandlerMapping()
}
}

@Bean
fun feignContract(
feignClientProperties: FeignClientProperties?,
parameterProcessors: List<AnnotatedParameterProcessor>,
feignConversionService: ConversionService,
): Contract {
val decodeSlash = feignClientProperties == null || feignClientProperties.isDecodeSlash
return CustomSpringMvcContract(parameterProcessors, feignConversionService, decodeSlash)
}
}
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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import org.springframework.web.bind.annotation.RequestMapping
*/
@Api("Sample 服务接口")
@Primary
@FeignClient("devops-kotlin-sample", contextId = "SampleClient", path = "/service/sample")
@FeignClient("devops-kotlin-sample", contextId = "SampleClient")
@RequestMapping("/service/sample")
interface SampleClient {

@ApiOperation("获取Sample")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package com.tencent.devops.sample.controller
import com.tencent.devops.api.pojo.Response
import com.tencent.devops.sample.client.SampleClient
import com.tencent.devops.sample.pojo.Sample
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

/**
* Sample Controller
*/
@RestController
@RequestMapping("/service/sample")
class SampleController : SampleClient {

override fun getSample(): Response<Sample> {
Expand Down

0 comments on commit f9782a1

Please sign in to comment.