假装和HAL /资源 [英] Feign and HAL/resources
问题描述
我有一个服务器通过spring-data-rest公开资源,据我所知,HAL或HATEOAS使用它。但是当我尝试将它与Feign结合使用时,我似乎无法注册被拾取的Jackson2HalModule。
I've got a server that exposes resources through spring-data-rest and this uses, as far as I understand HAL or HATEOAS. But when I try to use it in combination with Feign, I can't seem to be able to register a Jackson2HalModule that gets picked up.
我有什么需要将Feign客户端连接到新转换器?它是否使用了另一个ObjectMapper而不是我在这里使用的那个?
Is there something I have to do to connect the Feign "client" to the new converter? Does it use another ObjectMapper than the one I got here?
代码:
@Inject
public void configureObjectMapper(ObjectMapper mapper, RestTemplate template) {
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.registerModule(new Jackson2HalModule());
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
converter.setObjectMapper(mapper);
template.getMessageConverters().add(converter);
}
服务器响应:
{
"_links" : {
"self" : {
"href" : "http://localhost:13372/user{?page,size,sort}",
"templated" : true
},
"search" : {
"href" : "http://localhost:13372/user/search"
}
},
"_embedded" : {
"user" : [ {
"id" : "5567613e5da543dba4201950",
"version" : 0,
"created" : "2015-05-28T18:41:02.685Z",
"createdBy" : "system test",
"edited" : "2015-05-28T18:41:02.713Z",
"editedBy" : "system test",
"username" : "devuser",
"email" : "dev@test.com",
"roles" : [ "USER" ],
"_links" : {
"self" : {
"href" : "http://localhost:13372/user/5567613e5da543dba4201950"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
异常:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.PushbackInputStream@7b6c6e70; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:762)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:758)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:275)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:216)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:206)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:25)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
推荐答案
这对我有用:
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@SpringBootApplication
@EnableFeignClients
public class Application {
....
}
注意@EnableHypermediaSupport
Note the @EnableHypermediaSupport
@FeignClient(url = "http://localhost:8080")
public interface PersonClient {
@RequestMapping(method = RequestMethod.GET, value = "/persons")
Resources<Person> getPersons();
@RequestMapping(method = RequestMethod.GET, value = "/persons/{id}")
Resource<Person> getPerson(@PathVariable("id") long id);
}
我在这里创建了一个完整的例子: https://github.com/jtdev/spring-feign-data-rest-example
I have created a fully working example here: https://github.com/jtdev/spring-feign-data-rest-example
请注意,只需将maven pom从spring-boot切换到spring-cloud(不更改代码),可能很容易导致json错误。
Note that simply switchen the maven pom from spring-boot to spring-cloud (without changing code), may easily result in json errors.
这篇关于假装和HAL /资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!