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
3f34ccc8
Commit
3f34ccc8
authored
Feb 22, 2016
by
Matt Benson
Committed by
Dave Syer
Feb 23, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Feign expanders based on Spring conversion service
Automatically registers Spring conversion-based Feign Expanders when available Fixes gh-841
parent
05305568
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
0 deletions
+62
-0
SpringMvcContract.java
...mework/cloud/netflix/feign/support/SpringMvcContract.java
+39
-0
FeignClientTests.java
...framework/cloud/netflix/feign/valid/FeignClientTests.java
+23
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/support/SpringMvcContract.java
View file @
3f34ccc8
...
...
@@ -33,12 +33,15 @@ import org.springframework.cloud.netflix.feign.annotation.RequestParamParameterP
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.util.Assert
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
feign.Contract
;
import
feign.Feign
;
import
feign.MethodMetadata
;
import
feign.Param
;
import
static
feign
.
Util
.
checkState
;
import
static
feign
.
Util
.
emptyToNull
;
...
...
@@ -58,14 +61,24 @@ public class SpringMvcContract extends Contract.BaseContract {
private
final
Map
<
Class
<?
extends
Annotation
>,
AnnotatedParameterProcessor
>
annotatedArgumentProcessors
;
private
final
Map
<
String
,
Method
>
processedMethods
=
new
HashMap
<>();
private
final
ConversionService
conversionService
;
public
SpringMvcContract
()
{
this
(
Collections
.<
AnnotatedParameterProcessor
>
emptyList
());
}
public
SpringMvcContract
(
List
<
AnnotatedParameterProcessor
>
annotatedParameterProcessors
)
{
this
(
annotatedParameterProcessors
,
new
DefaultConversionService
());
}
public
SpringMvcContract
(
List
<
AnnotatedParameterProcessor
>
annotatedParameterProcessors
,
ConversionService
conversionService
)
{
Assert
.
notNull
(
annotatedParameterProcessors
,
"Parameter processors can not be null."
);
Assert
.
notNull
(
conversionService
,
"ConversionService can not be null."
);
List
<
AnnotatedParameterProcessor
>
processors
;
if
(!
annotatedParameterProcessors
.
isEmpty
())
{
...
...
@@ -75,6 +88,8 @@ public class SpringMvcContract extends Contract.BaseContract {
processors
=
getDefaultAnnotatedArgumentsProcessors
();
}
this
.
annotatedArgumentProcessors
=
toAnnotatedArgumentProcessorMap
(
processors
);
this
.
conversionService
=
conversionService
;
ConvertingExpander
.
CONVERSION_SERVICE
.
set
(
conversionService
);
}
@Override
...
...
@@ -183,6 +198,11 @@ public class SpringMvcContract extends Contract.BaseContract {
processParameterAnnotation
);
}
}
if
(
isHttpAnnotation
&&
data
.
indexToExpanderClass
().
get
(
paramIndex
)
==
null
&&
this
.
conversionService
.
canConvert
(
method
.
getParameterTypes
()[
paramIndex
],
String
.
class
))
{
data
.
indexToExpanderClass
().
put
(
paramIndex
,
ConvertingExpander
.
class
);
}
return
isHttpAnnotation
;
}
...
...
@@ -292,4 +312,23 @@ public class SpringMvcContract extends Contract.BaseContract {
}
}
public
static
class
ConvertingExpander
implements
Param
.
Expander
{
static
final
ThreadLocal
<
ConversionService
>
CONVERSION_SERVICE
=
new
ThreadLocal
<
ConversionService
>()
{
protected
ConversionService
initialValue
()
{
return
new
DefaultConversionService
();
}
};
private
final
ConversionService
conversionService
;
public
ConvertingExpander
()
{
this
.
conversionService
=
CONVERSION_SERVICE
.
get
();
}
@Override
public
String
expand
(
Object
value
)
{
return
conversionService
.
convert
(
value
,
String
.
class
);
}
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/valid/FeignClientTests.java
View file @
3f34ccc8
...
...
@@ -29,6 +29,7 @@ import java.lang.reflect.Proxy;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.Locale
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -105,6 +106,15 @@ public class FeignClientTests {
@Autowired
HystrixClient
hystrixClient
;
protected
enum
Arg
{
A
,
B
;
@Override
public
String
toString
()
{
return
name
().
toLowerCase
(
Locale
.
ENGLISH
);
}
}
@FeignClient
(
value
=
"localapp"
,
configuration
=
TestClientConfig
.
class
)
protected
interface
TestClient
{
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello"
)
...
...
@@ -130,6 +140,9 @@ public class FeignClientTests {
@RequestMapping
(
method
=
RequestMethod
.
HEAD
,
value
=
"/head"
)
ResponseEntity
head
();
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/tostring"
)
String
getToString
(
@RequestParam
(
"arg"
)
Arg
arg
);
}
public
static
class
TestClientConfig
{
...
...
@@ -271,6 +284,10 @@ public class FeignClientTests {
return
ResponseEntity
.
status
(
HttpStatus
.
NOT_FOUND
).
body
((
String
)
null
);
}
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/tostring"
)
String
getToString
(
@RequestParam
(
"arg"
)
Arg
arg
)
{
return
arg
.
toString
();
}
public
static
void
main
(
String
[]
args
)
{
new
SpringApplicationBuilder
(
Application
.
class
).
properties
(
...
...
@@ -376,6 +393,12 @@ public class FeignClientTests {
}
@Test
public
void
testConvertingExpander
()
{
assertEquals
(
Arg
.
A
.
toString
(),
testClient
.
getToString
(
Arg
.
A
));
assertEquals
(
Arg
.
B
.
toString
(),
testClient
.
getToString
(
Arg
.
B
));
}
@Test
public
void
testHystrixFallbackWorks
()
{
Hello
hello
=
hystrixClient
.
fail
();
assertNotNull
(
"hello was null"
,
hello
);
...
...
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