feign 调用注意事项

参考链接:

https://blog.csdn.net/wahaha13168/article/details/81211002

https://www.cnblogs.com/merryyou/p/11670171.html

https://www.freesion.com/article/5635224856/

 feign 接口返回流

服务提供者

@GetMapping("/{id}")
    public void queryJobInfoLogDetail(@PathVariable("id") Long id, HttpServletResponse response) {

        File file = new File("xxxxx");
        InputStream fileInputStream = new FileInputStream(file);
        OutputStream outStream;
        try {
            outStream = response.getOutputStream();

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, len);
            }
            fileInputStream.close();
            outStream.close();
            outStream.flush();
        } catch (IOException e) {
            log.error("exception", e);
        }
    }

client 客户端

@GetMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    feign.Response queryJobInfoLogDetail(@PathVariable("id") Long id);

服务消费者

@GetMapping("/{id}")
    public void queryJobInfoLogInfoList(@PathVariable("id") Long id, HttpServletResponse servletResponse) {

        Response response = apiServices.queryJobInfoLogDetail(id);
        Response.Body body = response.body();

        InputStream fileInputStream = null;
        OutputStream outStream;
        try {
            fileInputStream = body.asInputStream();
            outStream = servletResponse.getOutputStream();

            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, len);
            }
            fileInputStream.close();
            outStream.close();
            outStream.flush();
        } catch (Exception e) {

        }
    }

2.Could not resolve view with name xxx 

问题:spring会认为你请求的结果是一个html的视图,所以才抛出了上面的异常。

解决方法:在请求的方法上加上@ResponseBody,或者有需要在控制层上加 @RestController 注解

3. 上传文件

服务调用者

@PostMapping("/xxx/file")
public xx uploadOrderFilesToOSS(@ApiParam("附件") @RequestParam("file") MultipartFile[] file) {
   return xxxService.uploadOrderFilesToOSS(file);
}

Feign

@PostMapping(value = "/file", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
xxx uploadSigleFile(@RequestParam("path") String path, @RequestPart("file") MultipartFile file);

服务提供者

@PostMapping(value = "/file", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public xxx uploadSigleFile(@RequestParam("path") String path, @RequestPart("file") MultipartFile file) {
  return fileService.uploadFileToOSS(path, file);
}

4. feign  PathVariable获取文件后缀

今天在做一个返回图片接口,因鉴于通过get请求传参获取文件名和文件路径会出现严重漏洞问题,现采用直接以文件路径的方式访问文件下载,url这样的写法@RequestMapping(value = "/content/{fileName}", method = RequestMethod.GET)会导致文件类型丢失。

解决方案:

在@RequestMapping的value中使用SpEL来表示,value中的{fileName}换成{fileName:.+}。

@RequestMapping(value = "/content/{fileName:.+}", method = RequestMethod.GET)

比如我从图片服务器获取某一文件,路径是localhost:8080/file/test.jpg,

通过@PathVariable应该获取test.jpg,如果不做任何处理,结果获取到的是test。

解决方法:

@RequestMapping("/file/{filename:.+}")

5. feign 日期格式问题:Could not read document: Can not deserialize value of type java.util.Date from String

解决方法:

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")

若加了没反应,需要查看调用方日期配置

若 调用方配置文件设置了如下格式,则被调用方也需要设置成格式一致

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

附上postman 测试日期链接:https://blog.csdn.net/qq_41611829/article/details/114627705

 

posted @ 2021-06-02 10:15  zch-admin  阅读(404)  评论(0)    收藏  举报