feign请求传送实体类参数的一些摸索

    @PostMapping("dc")
    public String dc(HttpServletRequest request, @RequestBody User user){
        List<String> services = discoveryClient.getServices();
        services.forEach(item->{
            logger.info("Service:"+item+":");
        });
        String name = user.getName();

        return "service"+services+name;

    }

以上是服务提供方的代码,使用@RequestBody接受请求体中的参数

    @GetMapping("/consumer")
    public String dc(){
        User user = new User();
        user.setName("mck1");
        user.setAge(1);

        return eurekaClient.dc(user);
    }


@FeignClient("eureka-client")
public interface EurekaClient {

    @PostMapping("/dc")
    public String dc(@Param("user") User user);
}

以上是服务调用方的代码,使用了@Param注解来发送参数(经过我测试发现,这个注解中的名字随便写不影响接收,说明@RequestBody是接收请求体中的所有)

以上是post请求的形式

 

 

get请求的形式不能用这种方式,get请求应该用@RequestParam注解来传送普通参数

转载于:https://www.cnblogs.com/changeCode/p/11147420.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Feign发送POST请求时,可以通过多种方式参数,包括请求体、表单参数、路径参数请求头等。下面是一些常见的示例: 1. 请求参数:可以使用`@RequestBody`注解将参数对象作为请求体发送。示例代码如下: ```java @RequestMapping(method = RequestMethod.POST, value = "/api/endpoint") ResponseEntity<String> postWithRequestBody(@RequestBody RequestData requestData); ``` 2. 递表单参数:可以使用`@RequestParam`注解将参数作为表单参数发送。示例代码如下: ```java @RequestMapping(method = RequestMethod.POST, value = "/api/endpoint") ResponseEntity<String> postWithFormParams(@RequestParam("param1") String param1, @RequestParam("param2") String param2); ``` 3. 递路径参数:可以使用`@PathVariable`注解将参数作为路径参数发送。示例代码如下: ```java @RequestMapping(method = RequestMethod.POST, value = "/api/endpoint/{id}") ResponseEntity<String> postWithPathParam(@PathVariable("id") String id); ``` 4. 请求头:可以使用`@RequestHeader`注解将参数作为请求头发送。示例代码如下: ```java @RequestMapping(method = RequestMethod.POST, value = "/api/endpoint") ResponseEntity<String> postWithRequestHeader(@RequestHeader("Authorization") String authorization); ``` 对于返回值,Feign默认使用Spring的`ResponseEntity`作为返回类型。可以根据实际情况设置合适的泛型类型,以便正确处理返回值。示例代码如下: ```java @RequestMapping(method = RequestMethod.POST, value = "/api/endpoint") ResponseEntity<MyResponse> postAndGetResponse(); ``` 在上述示例中,`MyResponse`是自定义的响应对象类型。 以上是一些常见的参数递和返回值处理的示例,具体的使用方式可以根据实际情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值