问题背景
在使用Open Feign 调用其他远程服务接口的时候,如果返回结果类型是Page<T>
分页对象,则可能会抛出异常
@FeignClient(name = "remoteCallYzElasticsearchService", url = "${com.xxx.rpc.service.remote-call-elasticsearch-service.prevBaseUrl}")
public interface RemoteCallElasticsearchService {
@PostMapping(value = "/rpc-service/queryRemoteElkLogInfoPageList.do")
Page<ElkLogResponseVO> queryRemoteElkLogInfoPageList(@SpringQueryMap ElkLogQueryParam elkLogQueryParam,
@SpringQueryMap MyPageParam myPageParam
);
异常内容详情如下:
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.data.domain.Page` (no Creators, like default constructor, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1904)
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1349)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:274)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4674)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3682)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:380)
... 108 common frames omitted
问题分析
org.springframework.data.domain.Page<T>
是一个接口,没有默认的构造方法。
当jackson 反序列化Page<T>
对象的时候找不到默认的构造方法,从而导致反序列化失败,因此抛出异常。
换句话说,就是默认Jackson 无法返回Page<T>
类型的对象,因为不知道找不到默认的构造方法,不知道怎么反序列化
package org.springframework.data.domain;
import java.util.Collections;
import java.util.function.Function;
/**
* A page is a sublist of a list of objects. It allows gain information about the position of it in the containing
* entire list.
*
* @param <T>
* @author Oliver Gierke
*/
public interface Page<T> extends Slice<T> {
/**
* Creates a new empty {@link Page}.
*
* @return
* @since 2.0
*/
static <T> Page<T> empty() {
return empty(Pageable.unpaged());
}
/**
* Creates a new empty {@link Page} for the given {@link Pageable}.
*
* @param pageable must not be {@literal null}.
* @return
* @since 2.0
*/
static <T> Page<T> empty(Pageable pageable) {
return new PageImpl<>(Collections.emptyList(), pageable, 0);
}
/**
* Returns the number of total pages.
*
* @return the number of total pages
*/
int getTotalPages();
/**
* Returns the total amount of elements.
*
* @return the total amount of elements
*/
long getTotalElements();
/**
* Returns a new {@link Page} with the content of the current one mapped by the given {@link Function}.
*
* @param converter must not be {@literal null}.
* @return a new {@link Page} with the content of the current one mapped by the given {@link Function}.
* @since 1.10
*/
<U> Page<U> map(Function<? super T, ? extends U> converter);
}
解决方案:
解决方案就是告诉程序如何反序列化 Page 对象。
org.springframework.cloud:spring-cloud-openfeign-core:2.2.5.RELEASE
以上版本有效。
import com.fasterxml.jackson.databind.Module;
import org.springframework.cloud.openfeign.support.PageJacksonModule;
import org.springframework.cloud.openfeign.support.SortJacksonModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfigurationFactory {
@Bean
public Module pageJacksonModule() {
return new PageJacksonModule();
}
@Bean
public Module sortJacksonModule() {
return new SortJacksonModule();
}
}