使用Feign时,为了不写重复代码,需要写feign公共接口方便调用,这时候需要注意以下问题,以发邮件为例

定义公共接口

/**
* @author liuyalong
* @date 2020/10/12 17:42
* 定义Feign公用接口,注意 [不能] 在类上写@RequestMapping("/email")注解,不然会报Ambiguous mapping错误
*/
//@RequestMapping("/email") 不能在这里写!!!
public interface BaseMailController {
/**
* 在为Feign定义服务标准接口的时候,处理请求参数的方法参数,必须使用@RequestParam注解描述。
* 并且,无论方法参数名和请求参数名是否一致,都需要定义@RequestParam注解的value/name属性。
* 在Feign技术中,默认的发起POST请求的时候,请求的参数,都是在请求体中使用JSON数据传递的,不是name=value对传递的。
* 默认环境中,只要是Feign发起的POST请求,请求参数都是JSON数据。必须使用@RequestBody处理。
*/
@PostMapping("/email/sendEmail")
BaseCommonResult<Integer> sendEmail(
@RequestParam(value = "to") String[] to,
@RequestParam(value = "subject") String subject,
@RequestParam(value = "text") String text,
@RequestParam(value = "filenames", required = false) String[] filenames ); }

Feign调用

@RestController
public class SendEmailController implements BaseMailController { @Autowired
private MailServices mailServices; /**
*
在接口中,已经将请求URL和方法耦合到一起了。
* 所以在当前的控制器中,不能重复定义@RequestMapping来约束请求URL,
* 不需要再使用@PathVariable,但是POST请求的JSON数据还是要使用@RequestBody
*/
@Override
public BaseCommonResult<Integer> sendEmail(
@RequestParam(value = "to") String[] to,
@RequestParam(value = "subject") String subject,
@RequestParam(value = "text") String text,
@RequestParam(value = "filenames", required = false) String[] filenames ) {
EmailEntity emailEntity = new EmailEntity();
emailEntity.setFilenames(filenames);
emailEntity.setSubject(subject);
emailEntity.setText(text);
emailEntity.setTo(to); try {
mailServices.sendMail(emailEntity);
} catch (Exception e) {
e.printStackTrace();
return BaseCommonResult.failed("邮件发送失败");
} return BaseCommonResult.success(1, "邮件发送成功");
} }

最新文章

  1. 在线图片压缩后以ImageIO 流的形式 设置大小显示指定页面
  2. LightOJ 1104
  3. Java常用命令
  4. ubuntu su Authentication failure
  5. Top 10 Free Wireless Network hacking/monitoring tools for ethical hackers and businesses
  6. RegisterStartupScript和RegisterClientScriptBlock的用法
  7. Git教程之远程仓库(9)
  8. [MODx] 4. getResources
  9. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模
  10. Word2003中如何使封面和目录中不插入页码
  11. ELK初学搭建(logstash)
  12. wamp安装
  13. Linux的vi详解
  14. WMI Explorer操作 和 powershell命令
  15. UE4命令行使用,解释
  16. mysql基础篇(上篇)
  17. leetcode53
  18. myeclise 安装
  19. 这是一位拿到BAT大厂offer应届生的年终总结,那么你的呢?
  20. OneZero产品视频

热门文章

  1. hibernate 基础知识
  2. ubuntu服务器启动过程中重启卡死的问题解决
  3. 使用Actor模型管理Web Worker多线程
  4. bugku-PHP_encrypt_1(ISCCCTF)
  5. xml格式数据和数组数据互相转换
  6. xdebug不显示
  7. 深度解析:java必须掌握的知识点——类的重用
  8. ABBYY FineReader 15 如何为PDF文档添加页眉页脚
  9. JUC并发工具包之CyclicBarrier &amp; CountDownLatch的异同
  10. starUML软件破解