这篇文章介绍了EntityFramework表拆分为多个实体的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 正确写法如下
(1)方法1
@FeignClient(name = quot;microservice-provider-userquot;)public interface UserFeignClient {@RequestMapping(value = quot;/getquot;, method = RequestMethod.GET)public User get1(@RequestParam(quot;idquot;) Long id, @RequestParam(quot;usernamequot;) String username);}
这是最直观的方式。URL有几个参数,Feign接口中的方法有几个参数。使用@RequestParam批注指定请求的参数。
(2)方法二
@FeignClient(name = quot;microservice-provider-userquot;)public interface UserFeignClient {@RequestMapping(value = quot;/getquot;, method = RequestMethod.GET)public User get2(@RequestParam Maplt;String, Objectgt; map);}
也可以使用Map构建多参数URL。当目标URL参数非常大时,可以用这个方法来简化Feign接口的编写。
POST请求包含多个参数
让我们讨论如何使用Feign构造一个带有多个参数的POST请求。举个例子,假设我们用户微服务的控制器是这样写的:
@RestControllerpublic class UserController {@PostMapping(quot;/postquot;)public User post(@RequestBody User user) {...}}
我们的Feign接口怎么写?答案很简单,比如:
@FeignClient(name = quot;microservice-provider-userquot;)public interface UserFeignClient {@RequestMapping(value = quot;/postquot;, method = RequestMethod.POST)public User post(@RequestBody User user);}
feign接口调用的其他微服务是set对象(List
首先,如果传输的是一个集合对象,一般不是PUT或POST请求可以使用@ request param(quot;amphellipquot)是写在界面的新参数,如
@GetMapping(quot;/find/sec/consume/product/categoryquot;)public ResponseEntitylt;Listlt;SecConsumeProductCategoryVOgt;gt; getSecConsumeProductCategory(@RequestParam(quot;sellerIdsquot;) Listlt;Longgt; sellerIds){Listlt;SecConsumeProductCategoryVOgt; secConsumeProductCategories = secConsumeProductBaseBusinessService.getSecConsumeProductCategory(sellerIds);return ResponseEntity.ok(secConsumeProductCategories);}
对于feign调用和参数设置对象,在feign client中,可以使用以下方法。您必须使用@RequestMapping,而不是直接使用@PutMapping或@PostMapping,并且注释@RequestBody仍然用于形式参数。
@RequestMapping(value = quot;/cancel/daily/appointmentquot;,method = RequestMethod.PUT)public Void updateBatchDailyAppointment(@RequestBody Listlt;PdProductDailyAppointmentDTOgt; cancelAppointmentDTOS/*String cancelAppointmentStr*/);
对于被叫方,可以这样写
@RequestMapping(value = quot;/cancel/daily/appointmentquot;,method = RequestMethod.PUT)public ResponseEntitylt;Voidgt; updateBatchDailyAppointment(@RequestBody Listlt;PdProductDailyAppointmentDTOgt; cancelAppointmentDTOS/*String cancelAppointmentStr*/){pdProductDailyAppointmentBusinessService.updateBatchDailyAppointment(cancelAppointmentDTOS);return ResponseEntity.status(HttpStatus.CREATED).build();}
这样就可以解决佯调用传递集合对象的问题。
以上个人经验,希望能给你一个参考
精彩评论