1. 程式人生 > >微服務之間呼叫控制器註解型別的差異

微服務之間呼叫控制器註解型別的差異

今天在一個業務服務通過Feign呼叫檔案服務上傳檔案時遇到了幾個問題:

1. 提示http請求頭過大的問題;

  • 此時需要修改bootstrap.yml,加入
server:
 max-http-header-size: 10000000

用以放大尺寸

2. 呼叫方法時提示404,無返回結果;

  • 解決方法:把控制器的註解由@Controller變為@RestController,就可以

被呼叫方具體程式碼如下:

@Slf4j
@RestController
@RequestMapping("/image")
public class ImageController {
    private static List<String> allowUploadSuffixes = new ArrayList<>(Arrays.asList("png", "jpg", "jpeg", "zip", "pdf", "xls", "xlsx", "rar", "doc", "docx"));

    @Autowired
    private UploadFileEntityMapper uploadFileEntityMapper;

    @RequestMapping(value = "/uploadBase64", method = RequestMethod.POST)
    @ApiOperation(value = "通過base64方式上傳檔案")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "appId", dataType = "String", required = true, value = "應用ID"),
            @ApiImplicitParam(paramType = "form", name = "group", dataType = "String", required = true, value = "對應配置中心配置上傳組名(如:public,private等)"),
            @ApiImplicitParam(paramType = "form", name = "fileName", dataType = "String", required = true, value = "檔案原名稱"),
            @ApiImplicitParam(paramType = "form", name = "file", dataType = "String", required = true, value = "檔案內容")
    })
    public ApiResult<UploadResult> uploadBase64(@RequestParam("file") String file, @RequestParam(value = "appId") String appId, @RequestParam("group") String group, @RequestParam("fileName") String originFileName) {
        String imageBase64Str;
        String suffix;
        String mime;
        if (StringUtils.isBlank(file)) {
            return new ApiResult<>(new UploadResult());
        } else if (file.indexOf("data:image/png;") != -1) {
            imageBase64Str = file.replace("data:image/png;base64,", "");
            suffix = "png";
            mime = "image/png";
        } else if (file.indexOf("data:image/jpeg;") != -1) {
            imageBase64Str = file.replace("data:image/jpeg;base64,", "");
            suffix = "jpeg";
            mime = "image/jpeg";
        } else {
            return new ApiResult<>(new UploadResult());
        }
        try {
            if (!allowUploadSuffixes.contains(suffix)) {
                throw new IotBaseException(9999, "不允許上傳該檔案型別");
            }

            String fileKey = UUID.randomUUID().toString() + "." + suffix;

            FileSystemClient client = FileSystemClient.getClient(group);
            String url = client.upload(fileKey, Base64.getDecoder().decode(imageBase64Str), appId, originFileName);

            UploadFileEntity entity = new UploadFileEntity();
            entity.setAppId(appId);
            entity.setGroupName(group);
            entity.setFileName(originFileName);
            entity.setFileUrl(url);
            entity.setMimeType(mime);
            entity.setProvider(client.getProvider().name());
            entity.setCreatedAt(new Date());
            uploadFileEntityMapper.insert(entity);

            return new ApiResult<>(new UploadResult(url, originFileName));
        } catch (Exception e) {
            e.printStackTrace();
            throw new IotBaseException(ExceptionCode.SYSTEM_ERROR.code, "上傳失敗");