基于SpringBoot2.2.4框架搭建的项目中报错
一、mybatis中返回对象未进行序列化
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ser/impl/PropertySerializerMap$TypeAndSerializer
解决:当前mybatis返回对象没有进行序列化,需要将mybatis中返回值的接收对象实现Serializable
二、在使用OpenFeign远程调用其它服务时报错
Request processing failed; nested exception is feign.codec.EncodeException: Could not write JSON: Infinite recursion (StackOverflowError)
解决:返回的响应是自定义通用对象,但是需要对象有无参构造器,所以加上无参构造即可
三、OpenFeign不支持跨服务上传文件
加入支持Feign的文件上传的依赖
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.8.0</version>
</dependency>
在Feign的接口出加上配置文件
@FeignClient(value = "nacos-file", fallbackFactory = FallBack.class, configuration = FeignLogConfiguration.class)
@Component
public interface FeignService {
/**
* 调用文件服务
* @param file 需要上传的文件
* @param bucketName 桶名称
* @param fileType 文件类型
* @return 上传成功后的地址信息
*/
@PostMapping(value = "/file/upload",
produces = { MediaType.APPLICATION_JSON_UTF8_VALUE },
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
RespResult upload(@RequestPart(name = "file") MultipartFile file,
@RequestParam(name = "bucketName") String bucketName,
@RequestParam(name = "fileType") String fileType);
}
@Configuration
public class FeignLogConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
@Bean
@Primary
@Scope("prototype")
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
}
四、Feign上传MutlFile时报错:The current request is not a multipart request
原因:Feign在转发时,是以普通表单形式发送给对应服务,以ContentType=application/x-www-form-urlencoded的表单发送,而文件上传需form-data的ContentType,故服务端接收不到请求。
解决:在Feign接口上加上produces和consumes
@PostMapping(value = "/file/upload",
produces = { MediaType.APPLICATION_JSON_UTF8_VALUE },
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
RespResult upload(@RequestPart(name = "file") MultipartFile file,
@RequestParam(name = "bucketName") String bucketName,
@RequestParam(name = "fileType") String fileType);