其他分享
首页 > 其他分享> > 记录Springcloud feign 文件上传报错 FileUploadException: the request was rejected because no multipart bounda

记录Springcloud feign 文件上传报错 FileUploadException: the request was rejected because no multipart bounda

作者:互联网

记录一次使用feign接口调用第三方接口上传文件报错排错新得(项目环境比较老,新版本的SpringCloud SpringBoot使用没有出现问题)

项目环境

:: Spring Boot :: (v1.5.12.RELEASE)
spring-cloud-starter-1.3.3.RELEASE

pom

这里需要注意的是,需要找到你所有对应的fegin表单上传插件版本,之前我换的版本太高了也会导致识别不了

        <!-- fegin 接口调用文件上传 -->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

配置文件
package com.yyc.ewz.config;

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

/**
 * feign调用文件上传配置
 * @author pyf
 * @date 2022/7/5
 * @apiNote
 */
public class MultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

}

feignClient 调用方配置

需要注意


package com.yyc.ewz.common.remote;


import com.yyc.commons.base.vo.ResultData;
import com.yyc.commons.exception.BusinessException;
import com.yyc.ewz.config.MultipartSupportConfig;
import com.yyc.ewz.ax.domain.bo.AutographBo;
import com.yyc.ewz.ax.domain.bo.OverTimeConfirmBo;
import com.yyc.ewz.ax.domain.bo.SignInfoBo;
import com.yyc.ewz.ax.domain.bo.SignatureImgBo;
import com.yyc.ewz.ax.domain.vo.SignBusiDtlVo;
import com.yyc.ewz.ax.domain.vo.SignBusiResVO;
import com.yyc.ewz.common.remote.hystrix.SignRemoteServiceHystrix;
import feign.Response;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**签字签章接口
 * @author pyf
 * @date 2022/6/30
 * @apiNote
 **/
@FeignClient(value = "yyc-sign",fallback = SignRemoteServiceHystrix.class,configuration = MultipartSupportConfig.class)
public interface SignRemoteService {
    @ApiOperation(value = "上传接口", notes = "上传接口")
    @RequestMapping(value = "/sign/signfile/uploadSignPaper",consumes = MediaType.MULTIPART_FORM_DATA_VALUE,method = RequestMethod.POST)
    ResultData<Boolean> uploadSignPaper(@RequestPart(value = "file") MultipartFile file, @RequestParam("businessId")String businessId, @RequestParam("templateCode")String templateCode, @RequestParam("comSignBusiDtlIds")String comSignBusiDtlIds)throws BusinessException;
}
package com.yyc.ewz.common.remote.hystrix;


import com.yyc.commons.base.vo.ResResponse;
import com.yyc.commons.base.vo.ResultData;
import com.yyc.commons.exception.BusinessException;
import com.yyc.ewz.ax.domain.bo.AutographBo;
import com.yyc.ewz.ax.domain.bo.OverTimeConfirmBo;
import com.yyc.ewz.ax.domain.bo.SignInfoBo;
import com.yyc.ewz.ax.domain.bo.SignatureImgBo;
import com.yyc.ewz.ax.domain.vo.SignBusiDtlVo;
import com.yyc.ewz.ax.domain.vo.SignBusiResVO;
import com.yyc.ewz.common.remote.SignRemoteService;
import com.yyc.ewz.enums.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**接口兜底
 * @author pyf
 * @date 2022/6/30
 * @apiNote
 */
@Component
@Slf4j
public class SignRemoteServiceHystrix implements SignRemoteService {

    @Override
    public ResultData<Boolean> uploadSignPaper(MultipartFile file, String businessId, String templateCode, String comSignBusiDtlIds) throws BusinessException {
        log.error("上传单据失败!");
        return ResResponse.error(Response.FAILURE.getMessage());
    }


}

被调用方
package com.yyc.sign.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyc.commons.base.dto.ResResponse;
import com.yyc.commons.base.dto.ResultData;
import com.yyc.commons.exception.BusinessException;
import com.yyc.sign.domain.bo.SignInfoBo;
import com.yyc.sign.domain.SignBusi;
import com.yyc.sign.domain.SignFile;
import com.yyc.sign.domain.bo.UploadSignPaperBo;
import com.yyc.sign.service.SignBusiService;
import com.yyc.sign.service.SignFileService;
import com.yyc.sign.service.SignService;
import com.yyc.sign.utils.LocalFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * 文件表
 *
 * @author pyf pengyifeng@aliyun.com
 * @since 1.0.0 2022-06-13
 */
@Slf4j
@RestController
@RequestMapping("sign/signfile")
@Api(tags="API")
public class SignFileController {


    @Resource
    private SignService signService;



    @ApiOperation(value = "上传接口", notes = "上接口")
    @PostMapping(value = "/uploadSignPaper")
    public ResultData<Boolean> uploadSignPaper(@RequestParam("file") MultipartFile file,@Validated UploadSignPaperBo uploadSignPaperBo){
        try {
            return ResResponse.success(signService.uploadSignPaper(file,uploadSignPaperBo));
        } catch (IOException e) {
            log.error("上传签字签章单据失败msg{}"+e.getMessage(),e);
            return ResResponse.error("上传签字签章单据失败!");
        }
    }
}

标签:feign,because,springframework,ewz,报错,yyc,org,import,com
来源: https://www.cnblogs.com/pengyifeng/p/16445623.html