LG8*_*G87 4 jackson spring-data-rest spring-hateoas
我有一个服务器通过spring-data-rest公开资源,据我所知HAL或HATEOAS.但是当我尝试将它与Feign结合使用时,我似乎无法注册被拾取的Jackson2HalModule.
有什么东西要把Feign"客户端"连接到新的转换器吗?它使用的是另一个ObjectMapper吗?
码:
@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);
}
Run Code Online (Sandbox Code Playgroud)
服务器响应:
{
"_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
}
}
Run Code Online (Sandbox Code Playgroud)
例外:
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)
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题.由于REST API的响应是单个响应,因此发生了异常.因此它未能将其视为实体列表.
当我添加(基于上面的代码):
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
Run Code Online (Sandbox Code Playgroud)
有用
编辑:在旁注中,我已经实现了FeignClient,如下所示:
@Service
@FeignClient(UsersConstants.USER_SERVICE_NAME)
public interface UsersServices {
@RequestMapping(method = RequestMethod.GET, value = "/user")
List<User> getUsers();
}
Run Code Online (Sandbox Code Playgroud)
但它应该如何,因为它是一个可分页的资源:
@Service
@FeignClient(UsersConstants.USER_SERVICE_NAME)
public interface UsersServices {
@RequestMapping(method = RequestMethod.GET, value = "/user")
List<PagedResources<User>> getUsers();
}
Run Code Online (Sandbox Code Playgroud)
PagedResource位于HATEOAS依赖项中:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
它还有许多其他类可以提供帮助,例如资源,资源等.
这对我有用:
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
@SpringBootApplication
@EnableFeignClients
public class Application {
....
}
Run Code Online (Sandbox Code Playgroud)
请注意@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);
}
Run Code Online (Sandbox Code Playgroud)
我在这里创建了一个完整的例子:https://github.com/jtdev/spring-feign-data-rest-example
请注意,简单地将maven pom从spring-boot切换到spring-cloud(不更改代码),可能很容易导致json错误.
| 归档时间: |
|
| 查看次数: |
5412 次 |
| 最近记录: |