gpt4 book ai didi

jackson - Feign 和 HAL/资源

转载 作者:行者123 更新时间:2023-12-04 22:11:24 24 4
gpt4 key购买 nike

我有一个通过 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);
}

来自服务器的响应:
{
"_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
}
}

异常(exception):
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)

最佳答案

我发现了问题。
由于来自 REST API 的响应是单个响应,因此发生异常。所以它未能将其视为实体列表。

当我添加时(基于上面的代码):

mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

有用

编辑:
附带说明一下,我已经像这样实现了 FeignClient:
@Service
@FeignClient(UsersConstants.USER_SERVICE_NAME)
public interface UsersServices {

@RequestMapping(method = RequestMethod.GET, value = "/user")
List<User> getUsers();
}

但它应该如何,因为它是一个可分页的资源:
@Service
@FeignClient(UsersConstants.USER_SERVICE_NAME)
public interface UsersServices {

@RequestMapping(method = RequestMethod.GET, value = "/user")
List<PagedResources<User>> getUsers();
}

PagedResource 位于 HATEOAS 依赖项中:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>

它还有很多其他类可以提供帮助,例如 Resource、Resources 等。

关于jackson - Feign 和 HAL/资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com