伪装客户端:DecodeException:提取响应时出错

问题描述 投票:0回答:1

我正在微服务架构中使用Open Feign和Hateos。当我使用Feign客户端获取内容时,出现以下错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is feign.codec.DecodeException: Error while extracting response for type [java.util.List<com.nyota.nyotaplatform.model.asset.Product>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.time.LocalDateTime` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.time.LocalDateTime` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1582] (through reference chain: java.util.ArrayList[1]->com.nyota.nyotaplatform.model.asset.Product["vendor"]->com.nyota.nyotaplatform.model.asset.Vendor["kyc"]->java.util.ArrayList[0]->com.nyota.nyotaplatform.model.fsp.Kyc["document"]->com.nyota.nyotaplatform.model.fsp.Document["uploadDateTime"])] with root cause

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.time.LocalDateTime` out of START_ARRAY token
 at [Source: (PushbackInputStream); line: 1, column: 1582] (through reference chain: java.util.ArrayList[1]->com.nyota.nyotaplatform.model.asset.Product["vendor"]->com.nyota.nyotaplatform.model.asset.Vendor["kyc"]->java.util.ArrayList[0]->com.nyota.nyotaplatform.model.fsp.Kyc["document"]->com.nyota.nyotaplatform.model.fsp.Document["uploadDateTime"])

下面是我的假客户:

@FeignClient(name="asset-market")
public interface AssetMarketClient {

    @RequestMapping(path = "/product/filterProduct", method = RequestMethod.POST)
    Page<Product> getfilterProduct(@RequestBody ProductFilter filter);

    @RequestMapping(path = "/product/getProducts", method = RequestMethod.GET)
    List<Product> getProducts();

}

下面我提供Feign客户端配置:

@Bean
    public ClientCredentialsResourceDetails clientCredentialsResourceDetails() {
        ClientCredentialsResourceDetails resource = new ClientCredentialsResourceDetails();
        resource.setAccessTokenUri(serverUrl);
        resource.setClientId(clientId);
        resource.setClientSecret(secret);
        return resource;
    }

    @Bean
    public RequestInterceptor oauth2FeignRequestInterceptor(){
        return new OAuth2FeignRequestInterceptor(new DefaultOAuth2ClientContext(), clientCredentialsResourceDetails());
    }

    @Bean
    public RestTemplate oAuthRestTemplate() {
        DefaultOAuth2ClientContext clientContext = new DefaultOAuth2ClientContext();
        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(clientCredentialsResourceDetails(), clientContext);
        return restTemplate;
    }

    @Bean
    public RequestContextListener requestContextListener() {
        return new RequestContextListener();
    }

任何人都可以告诉我我在哪里犯错,或者我还需要配置什么才能实现这一目标?

java java-8 microservices spring-cloud spring-cloud-feign
1个回答
0
投票

很抱歉,迟交此答案。杂耍后,我发现feign client完全正常。主要问题在于解析接收到的JSON对象。它包含一个Jackson无法解析的时间实体,因此我在文档类中使用了以下代码。

@JsonSerialize(using = LocalTimeSerializer.class)
@JsonDeserialize(using = LocalTimeDeserializer.class)
private LocalTime sendOn;

解决了我的问题。希望它也对其他人有帮助...!

© www.soinside.com 2019 - 2024. All rights reserved.