###一、 提供方
(1)接口定义
@FeignClient(value = "spring.application.name") //服务名
public interface IArticleClient {
@PostMapping("/api/v1/article/save")
public ResponseResult saveArticle(@RequestBody ArticleDto dto) ;
@GetMapping("/api/v1/article/findArticleConfigByArticleId/{articleId}")
ResponseResult findArticleConfigByArticleId(@PathVariable("articleId") Long articleId);
@PostMapping("/api/v1/article/findNewsComments")
public PageResponseResult findNewsComments(@RequestBody ArticleCommentDto dto);
@PostMapping("/api/v1/article/updateCommentStatus")
public ResponseResult updateCommentStatus(@RequestBody CommentConfigDto dto);
@PostMapping("/api/v1/article/newPage")
PageResponseResult newPage(@RequestBody StatisticsDto dto);
@GetMapping("/api/v1/article/queryLikesAndConllections")
ResponseResult queryLikesAndConllections(@RequestParam("wmUserId") Integer wmUserId, @RequestParam("beginDate") Date beginDate, @RequestParam("endDate") Date endDate);
}
(2)接口实现
@RestController
public class ArticleClient implements IArticleClient {
@Autowired
ApArticleService apArticleService;
@PostMapping("/api/v1/article/save")
public ResponseResult saveArticle(@RequestBody ArticleDto dto) {
return apArticleService.saveArticle(dto);
}
@Autowired
private ApArticleConfigService apArticleConfigService;
@GetMapping("/api/v1/article/findArticleConfigByArticleId/{articleId}")
@Override
public ResponseResult findArticleConfigByArticleId(@PathVariable("articleId") Long articleId) {
ApArticleConfig apArticleConfig = apArticleConfigService.getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId));
return ResponseResult.okResult(apArticleConfig);
}
@PostMapping("/api/v1/article/findNewsComments")
@Override
public PageResponseResult findNewsComments(@RequestBody ArticleCommentDto dto) {
return apArticleService.findNewsComments(dto);
}
@PostMapping("/api/v1/article/updateCommentStatus")
@Override
public ResponseResult updateCommentStatus(@RequestBody CommentConfigDto dto) {
return apArticleConfigService.updateCommentStatus(dto);
}
@PostMapping("/api/v1/article/newPage")
@Override
public PageResponseResult newPage(@RequestBody StatisticsDto dto) {
return apArticleService.newPage(dto);
}
@GetMapping("/api/v1/article/queryLikesAndConllections")
@Override
public ResponseResult queryLikesAndConllections(@RequestParam("wmUserId") Integer wmUserId,@RequestParam("beginDate") Date beginDate,@RequestParam("endDate") Date endDate) {
return apArticleService.queryLikesAndConllections(wmUserId,beginDate,endDate);
}
}
(3) 启动类上开启feign调用
@EnableFeignClients(basePackages = "com.xxx.apis")
(4) 解释
1和2 接口定义与接口实现视项目情况可以分别放在不同的项目里。如放在不同的项目中,注意实现前,进行依赖的添加。
注意 :提供方做 feign 的开启(@FeignClient)
二 、使用方
(1) 接口注入
@Autowired
private IArticleClient articleClient;
(2)方法调用
articleClient.findArticleConfigByArticleId(articleId);