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
8756a888
Commit
8756a888
authored
Nov 05, 2014
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow feign to decode parameterized types. Added SpringDecoderTests.
fixes gh-57
parent
bebcf660
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
116 additions
and
5 deletions
+116
-5
SpringDecoder.java
...rg/springframework/cloud/netflix/feign/SpringDecoder.java
+5
-5
SpringDecoderTests.java
...ringframework/cloud/netflix/feign/SpringDecoderTests.java
+111
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/SpringDecoder.java
View file @
8756a888
...
@@ -2,6 +2,7 @@ package org.springframework.cloud.netflix.feign;
...
@@ -2,6 +2,7 @@ package org.springframework.cloud.netflix.feign;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.lang.reflect.ParameterizedType
;
import
java.lang.reflect.Type
;
import
java.lang.reflect.Type
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -31,15 +32,14 @@ public class SpringDecoder implements Decoder {
...
@@ -31,15 +32,14 @@ public class SpringDecoder implements Decoder {
@Override
@Override
public
Object
decode
(
final
Response
response
,
Type
type
)
throws
IOException
,
FeignException
{
public
Object
decode
(
final
Response
response
,
Type
type
)
throws
IOException
,
FeignException
{
if
(
type
instanceof
Class
)
{
if
(
type
instanceof
Class
||
type
instanceof
ParameterizedType
)
{
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
HttpMessageConverterExtractor
<?>
extractor
=
new
HttpMessageConverterExtractor
(
HttpMessageConverterExtractor
<?>
extractor
=
new
HttpMessageConverterExtractor
(
(
Class
<?>)
type
,
messageConverters
.
getConverters
());
type
,
messageConverters
.
getConverters
());
Object
data
=
extractor
.
extractData
(
new
FeignResponseAdapter
(
response
));
return
extractor
.
extractData
(
new
FeignResponseAdapter
(
response
));
return
data
;
}
}
throw
new
DecodeException
(
"type is not an instance of Class: "
+
type
);
throw
new
DecodeException
(
"type is not an instance of Class
or ParameterizedType
: "
+
type
);
}
}
private
class
FeignResponseAdapter
implements
ClientHttpResponse
{
private
class
FeignResponseAdapter
implements
ClientHttpResponse
{
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/SpringDecoderTests.java
0 → 100644
View file @
8756a888
package
org
.
springframework
.
cloud
.
netflix
.
feign
;
import
static
org
.
junit
.
Assert
.*;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.builder.SpringApplicationBuilder
;
import
org.springframework.boot.test.IntegrationTest
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.web.WebAppConfiguration
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* @author Spencer Gibb
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
SpringDecoderTests
.
Application
.
class
)
@WebAppConfiguration
@IntegrationTest
({
"server.port=0"
,
"spring.application.name=springdecodertest"
,
"spring.jmx.enabled=true"
})
public
class
SpringDecoderTests
extends
FeignConfigurer
{
@Value
(
"${local.server.port}"
)
private
int
port
=
0
;
public
TestClient
testClient
()
{
return
feign
().
target
(
TestClient
.
class
,
"http://localhost:"
+
port
);
}
protected
static
interface
TestClient
{
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hello"
)
public
Hello
getHello
();
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hellos"
)
public
List
<
Hello
>
getHellos
();
@RequestMapping
(
method
=
RequestMethod
.
GET
,
value
=
"/hellostrings"
)
public
List
<
String
>
getHelloStrings
();
}
@Configuration
@EnableAutoConfiguration
@RestController
protected
static
class
Application
implements
TestClient
{
public
Hello
getHello
()
{
return
new
Hello
(
"hello world 1"
);
}
public
List
<
Hello
>
getHellos
()
{
ArrayList
<
Hello
>
hellos
=
new
ArrayList
<>();
hellos
.
add
(
new
Hello
(
"hello world 1"
));
hellos
.
add
(
new
Hello
(
"oi terra 2"
));
return
hellos
;
}
public
List
<
String
>
getHelloStrings
()
{
ArrayList
<
String
>
hellos
=
new
ArrayList
<>();
hellos
.
add
(
"hello world 1"
);
hellos
.
add
(
"oi terra 2"
);
return
hellos
;
}
public
static
void
main
(
String
[]
args
)
{
new
SpringApplicationBuilder
(
Application
.
class
).
properties
(
"spring.application.name=springdecodertest"
,
"management.contextPath=/admin"
)
.
run
(
args
);
}
}
@Test
public
void
testSimpleType
()
{
Hello
hello
=
testClient
().
getHello
();
assertNotNull
(
"hello was null"
,
hello
);
assertEquals
(
"first hello didn't match"
,
new
Hello
(
"hello world 1"
),
hello
);
}
@Test
public
void
testUserParameterizedTypeDecode
()
{
List
<
Hello
>
hellos
=
testClient
().
getHellos
();
assertNotNull
(
"hellos was null"
,
hellos
);
assertEquals
(
"hellos was not the right size"
,
2
,
hellos
.
size
());
assertEquals
(
"first hello didn't match"
,
new
Hello
(
"hello world 1"
),
hellos
.
get
(
0
));
}
@Test
public
void
testSimpleParameterizedTypeDecode
()
{
List
<
String
>
hellos
=
testClient
().
getHelloStrings
();
assertNotNull
(
"hellos was null"
,
hellos
);
assertEquals
(
"hellos was not the right size"
,
2
,
hellos
.
size
());
assertEquals
(
"first hello didn't match"
,
"hello world 1"
,
hellos
.
get
(
0
));
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public
static
class
Hello
{
private
String
message
;
}
}
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