Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
2b409524
Commit
2b409524
authored
Apr 23, 2016
by
Pedro Alvarado
Committed by
Dave Syer
Apr 25, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for placeholders to Feign spring-mvc RequestMapping annotation.
Fixes gh-894
parent
8b306df1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
5 deletions
+44
-5
SpringMvcContract.java
...mework/cloud/netflix/feign/support/SpringMvcContract.java
+26
-3
FeignClientTests.java
...framework/cloud/netflix/feign/valid/FeignClientTests.java
+15
-0
application.yml
spring-cloud-netflix-core/src/test/resources/application.yml
+3
-2
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/support/SpringMvcContract.java
View file @
2b409524
...
...
@@ -31,12 +31,17 @@ import org.springframework.cloud.netflix.feign.AnnotatedParameterProcessor;
import
org.springframework.cloud.netflix.feign.annotation.PathVariableParameterProcessor
;
import
org.springframework.cloud.netflix.feign.annotation.RequestHeaderParameterProcessor
;
import
org.springframework.cloud.netflix.feign.annotation.RequestParamParameterProcessor
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.ResourceLoaderAware
;
import
org.springframework.core.DefaultParameterNameDiscoverer
;
import
org.springframework.core.ParameterNameDiscoverer
;
import
org.springframework.core.annotation.AnnotationUtils
;
import
org.springframework.core.convert.ConversionService
;
import
org.springframework.core.convert.support.DefaultConversionService
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.ResourceLoader
;
import
org.springframework.util.Assert
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
feign.Contract
;
...
...
@@ -51,7 +56,7 @@ import static org.springframework.core.annotation.AnnotatedElementUtils.findMerg
/**
* @author Spencer Gibb
*/
public
class
SpringMvcContract
extends
Contract
.
BaseContract
{
public
class
SpringMvcContract
extends
Contract
.
BaseContract
implements
ResourceLoaderAware
{
private
static
final
String
ACCEPT
=
"Accept"
;
...
...
@@ -64,6 +69,7 @@ public class SpringMvcContract extends Contract.BaseContract {
private
final
ConversionService
conversionService
;
private
final
Param
.
Expander
expander
;
private
ResourceLoader
resourceLoader
=
new
DefaultResourceLoader
();
public
SpringMvcContract
()
{
this
(
Collections
.<
AnnotatedParameterProcessor
>
emptyList
());
...
...
@@ -95,6 +101,11 @@ public class SpringMvcContract extends Contract.BaseContract {
}
@Override
public
void
setResourceLoader
(
ResourceLoader
resourceLoader
)
{
this
.
resourceLoader
=
resourceLoader
;
}
@Override
public
MethodMetadata
parseAndValidateMetadata
(
Class
<?>
targetType
,
Method
method
)
{
this
.
processedMethods
.
put
(
Feign
.
configKey
(
targetType
,
method
),
method
);
MethodMetadata
md
=
super
.
parseAndValidateMetadata
(
targetType
,
method
);
...
...
@@ -108,6 +119,7 @@ public class SpringMvcContract extends Contract.BaseContract {
checkState
(
pathValue
!=
null
,
"RequestMapping.value() was empty on type %s"
,
method
.
getDeclaringClass
().
getName
());
pathValue
=
resolve
(
pathValue
);
if
(!
pathValue
.
startsWith
(
"/"
))
{
pathValue
=
"/"
+
pathValue
;
}
...
...
@@ -148,6 +160,7 @@ public class SpringMvcContract extends Contract.BaseContract {
if
(
methodMapping
.
value
().
length
>
0
)
{
String
pathValue
=
emptyToNull
(
methodMapping
.
value
()[
0
]);
if
(
pathValue
!=
null
)
{
pathValue
=
resolve
(
pathValue
);
// Append path from @RequestMapping if value is present on method
if
(!
pathValue
.
startsWith
(
"/"
)
&&
!
data
.
template
().
toString
().
endsWith
(
"/"
))
{
...
...
@@ -169,6 +182,15 @@ public class SpringMvcContract extends Contract.BaseContract {
data
.
indexToExpander
(
new
LinkedHashMap
<
Integer
,
Param
.
Expander
>());
}
private
String
resolve
(
String
value
)
{
if
(
StringUtils
.
hasText
(
value
)
&&
this
.
resourceLoader
instanceof
ConfigurableApplicationContext
)
{
return
((
ConfigurableApplicationContext
)
this
.
resourceLoader
).
getEnvironment
()
.
resolvePlaceholders
(
value
);
}
return
value
;
}
private
void
checkAtMostOne
(
Method
method
,
Object
[]
values
,
String
fieldName
)
{
checkState
(
values
!=
null
&&
(
values
.
length
==
0
||
values
.
length
==
1
),
"Method %s can only contain at most 1 %s field. Found: %s"
,
...
...
@@ -312,7 +334,7 @@ public class SpringMvcContract extends Contract.BaseContract {
@Override
public
Collection
<
String
>
setTemplateParameter
(
String
name
,
Collection
<
String
>
rest
)
{
Collection
<
String
>
rest
)
{
return
addTemplatedParam
(
rest
,
name
);
}
}
...
...
@@ -331,4 +353,4 @@ public class SpringMvcContract extends Contract.BaseContract {
}
}
}
}
\ No newline at end of file
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientTests.java
View file @
2b409524
...
...
@@ -142,6 +142,9 @@ public class FeignClientTests {
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello"
)
Hello
getHello
();
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"${feignClient.methodLevelRequestMappingPath}"
)
Hello
getHelloUsingPropertyPlaceHolder
();
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello"
)
Single
<
Hello
>
getHelloSingle
();
...
...
@@ -309,6 +312,11 @@ public class FeignClientTests {
return
new
Hello
(
HELLO_WORLD_1
);
}
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello2"
)
public
Hello
getHello2
()
{
return
new
Hello
(
OI_TERRA_2
);
}
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hellos"
)
public
List
<
Hello
>
getHellos
()
{
ArrayList
<
Hello
>
hellos
=
getHelloList
();
...
...
@@ -399,6 +407,13 @@ public class FeignClientTests {
}
@Test
public
void
testRequestMappingClassLevelPropertyReplacement
()
{
Hello
hello
=
this
.
testClient
.
getHelloUsingPropertyPlaceHolder
();
assertNotNull
(
"hello was null"
,
hello
);
assertEquals
(
"first hello didn't match"
,
new
Hello
(
OI_TERRA_2
),
hello
);
}
@Test
public
void
testSimpleType
()
{
Hello
hello
=
this
.
testClient
.
getHello
();
assertNotNull
(
"hello was null"
,
hello
);
...
...
spring-cloud-netflix-core/src/test/resources/application.yml
View file @
2b409524
...
...
@@ -42,4 +42,5 @@ zuul:
url
:
http://localhost:8081
path
:
/stores/**
feignClient
:
localappName
:
localapp
\ No newline at end of file
localappName
:
localapp
methodLevelRequestMappingPath
:
/hello2
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment