微服務之間調用控制器註解類型的差別

今天在一個業務服務經過Feign調用文件服務上傳文件時遇到了幾個問題:bootstrap

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

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

用以放大尺寸app

2. 調用方法時提示404,無返回結果;

  • 解決方法:把控制器的註解由@Controller變爲@RestController,就能夠

被調用方具體代碼以下:dom

@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, "上傳失敗");
        }
    }
}
相關文章
相關標籤/搜索