基于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);

五、待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值