Feign调用无法解析 IPage包裹的数据问题
AI-摘要
Tianli GPT
AI初始化中...
介绍自己
生成本文简介
推荐相关文章
前往主页
前往tianli博客
Feign调用无法解析 IPage包裹的数据,目前解决方案有两种,一种是转Page,另一种是序列化。
一、转Page传递
// api接口
@PostMapping(value = "/queryEnterprise")
public Result<Page<StudentEnterprise>> queryEnterprise(@RequestBody StuEnterPriseQo qoo) {
StudentEnterprise qo = new StudentEnterprise().setDeleted(0);
if(Objects.nonNull(qoo.getName())){
qo.setName(qoo.getName());
}
QueryWrapper<StudentEnterprise> queryWrapper = QueryGenerator.initQueryWrapper(qo, null);
Page<StudentEnterprise> page = new Page<>(qoo.getPageNo(), qoo.getPageSize());
Page<StudentEnterprise> pageList = studentEnterpriseService.page(page, queryWrapper);
return Result.OK(pageList);
}
//feign调用
@PostMapping("/studentEnterprise/queryEnterprise")
@ApiOperation(value = "企业信息列表", notes = "企业信息列表")
JsonResult<Page<StudentEnterpriseVo>> queryEnterprise(@RequestBody StuEnterPriseQo qo);
二、序列化,在config文件夹添加如下代码。
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
public class IPageDeserializer extends StdDeserializer<IPage> {
protected IPageDeserializer(Class<?> vc) {
super(vc);
}
@Override
public IPage deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.getCodec().readTree(p);
String s = node.toString();
ObjectMapper om = new ObjectMapper();
Page page = om.readValue(s,Page.class);
return page;
}
}
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.Arrays;
import java.util.List;
@Configuration
public class WebDataConvertConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(mappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SimpleModule module = new SimpleModule();
module.addDeserializer(IPage.class, new IPageDeserializer(IPage.class));
mapper.registerModule(module);
converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON_UTF8, MediaType.APPLICATION_OCTET_STREAM));
converter.setObjectMapper(mapper);
return converter;
}
}
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果