Java中 springcloud.openfeign应用案例解析
作者:被程序耽误的胡先生
使用OpenFeign能让编写Web Service客户端更加简单,使用时只需定义服务接口,然后在上面添加注解,OpenFeign也支持可拔插式的编码和解码器,这篇文章主要介绍了Java中 springcloud.openfeign应用案例解析,需要的朋友可以参考下
OpenFeign是一个显示声明式的WebService客户端。使用OpenFeign能让编写Web Service客户端更加简单。使用时只需定义服务接口,然后在上面添加注解。OpenFeign也支持可拔插式的编码和解码器。spring cloud对feign进行了封装,使其支持MVC注解和HttpMessageConverts。和eureka(服务注册中心)和ribbon组合可以实现负载均衡。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,非常的方便。
1 导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.1.7</version> </dependency>
2 openfeign接口类编写
package com.sf.gis.taskmanage.event.feign; import com.alibaba.fastjson.JSONObject; import com.sf.gis.common.domain.dto.*; import com.sf.gis.common.domain.vo.*; import feign.Response; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @FeignClient(contextId = "eventAllocationNewServiceFegin", name = "eventAllocationNewServiceFegin", url = "${newevent.prefixurl}") public interface EventAllocationNewService { /** * 1. POST 获取工单号 * @param dto * @param token * @param type * @return */ @PostMapping(value = "/order/cOrderNumberRule/getNum", consumes = "application/json", produces = "application/json") EventCommonsVo workOrderNumber(@RequestBody WorkOrderNumberDto dto, @RequestHeader("Authorization") String token, @RequestHeader("content-type") String type); /* EventCommonVo<WorkOrderNumberVo> workOrderNumber(@RequestBody WorkOrderNumberDto dto, @RequestHeader("token") String token, @RequestHeader("content-type") String type); */ /** * 2创建工单接口 * @param dto * @param token * @param type * @return */ @PostMapping(value = "/order/cOrderNumberInfo/create", consumes = "application/json", produces = "application/json") EventCommonsVo createWorkOrder(@RequestBody CreateWorkOrderDto dto, @RequestHeader("Authorization") String token, @RequestHeader("content-type") String type); /** * 3 工单流转 * @paramwanderAboutDto * @param token * @param type * @return */ @PostMapping(value = "/order/cOrderCirculation/create", consumes = "application/json", produces = "application/json") EventCommonsVo wanderAbout(@RequestBody ReReportingDto reReportingDto, @RequestHeader("Authorization") String token, @RequestHeader("content-type") String type); /** * 工单处置信息更新接口 * @param workOrderUpdateDto * @param token * @param type * @return */ @PostMapping(value = "/order/cOrderNumberInfo/updateProcessInfo", consumes = "application/json", produces = "application/json") EventCommonVo workOrderUpdate(@RequestBody WorkOrderUpdateDto workOrderUpdateDto, @RequestHeader("Authorization") String token, @RequestHeader("Content-Type") String type); /** * 工单处置信息更新接口 * @param * @param token * @param type * @return */ @PostMapping("/order/cOrderNumberInfo/updateProcessInfo") EventCommonVo workOrderCompleteUpdate(@RequestBody WorkOrderCompleteUpdateDto workOrderCompleteUpdateDto, @RequestHeader("Authorization") String token, @RequestHeader("content-type") String type); /** * 上传附件 */ @RequestMapping(method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE,value = "/minio/upload") JSONObject upload(@RequestPart("file") MultipartFile file,@PathVariable("fileName") String fileName, @PathVariable("orderNum")String orderNum, @RequestHeader("Authorization") String token); /** * 附件下载 */ @RequestMapping(method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE,value = "/minio/download") Response download(@PathVariable("filename") String filename, @PathVariable("orderNum") String orderNum, @RequestHeader("Authorization") String token); /** * 工单状态变更接口(对接到市里) * @param * @param token * @param type * @return */ @PostMapping(value = "/order/cOrderStatusChange/sendMsg", consumes = "application/json", produces = "application/json") EventCommonVo orderStateUpdate(@RequestBody OrderChangeDto orderChangeDto, @RequestHeader("Authorization") String token, @RequestHeader("content-type") String type); }
3 URL 路径定义
url = "${newevent.prefixurl}")
到此这篇关于Java中 springcloud.openfeign应用案例解析的文章就介绍到这了,更多相关Java springcloud.openfeign内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!