feign 采坑之 not annotated with HTTP method type (ex. GET, POST)

研习springboot的feign时,遇到了这样的一个坑,由于本人愚钝,特记载下来方便以后翻阅。

问题描述:配置了FeignConfiguration,里面仅仅做了eureka的权限处理,like this:

@Configuration
public class FeignConfiguration{
    //为FeignConfiguration添加链接eureka的权限
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("seven", "pwd123456");
    }

}

然后在FeignClient中声明开放接口

@FeignClient(name="sss",url="localhost:8761",configuration=FeignConfiguration.class)
public interface IfeignInter {
    @RequestLine("GET /eureka/apps/{serviceName}")
    public String getServiceInfoByserviceName(@Param("serviceName") String serviceName) ;

}

然后启动服务报错:Method getServiceInfoByserviceName  not annotated with HTTP method type (ex. GET, POST)

各类坑的解决方法如下:

在spingboot中定义feign的对外开放接口:

@FeignClient(name="sss",url="localhost:8761",configuration=FeignConfiguration.class)
public interface IfeignInter {

//1.@GetMapping("/eureka/apps/{serviceName}")@PathVariable("serviceName")

//2.@RequestMapping(value="/eureka/apps/{serviceName}", method=RequestMethod.GET)@PathVariable("必须有值")

//3.@RequestLine("GET /eureka/apps/{serviceName}")@Param("serviceName")

@RequestLine("GET /eureka/apps/{serviceName}")
public String getServiceInfoByserviceName(@Param("serviceName") String serviceName) ;

}

自定义feign的config:

@Configuration
public class FeignConfiguration{
//使用feign的注解方式
@Bean
public Contract useFeignAnnotations() {
   return new Contract.Default();
}

//为FeignConfiguration添加链接eureka的权限
    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("seven", "pwd123456");
    }

}

1.不支持@GetMapping这样的混合注解只能使用@RequestMapping(value="/simple/{id}",method=RequestMethod.GET)这种方式来实现混合注解。

2.对于url路径上的PathVariable属性必须要有默认值,否者会报PathVariable annotation was empty on param 0.

3.这个就是当时我纠结了两个小时的坑,最后翻看Contract.Default()的源码才明白,如果FeignClient想要使用feign自己定义的注解,需要在configuration中配置feign的契约模式,因为其默认采用的时sping的注解方式,所以会不识别feign的注解,导致Method getServiceInfoByserviceName  not annotated with HTTP method type (ex. GET, POST)。

这种异常情况下的解决方法有两种:

一种就是使用sping默认的注解方式

@RequestMapping(value="/eureka/apps/{serviceName}", method=RequestMethod.GET)@PathVariable("必须有值")

eg:

@RequestMapping(value="/eureka/apps/{serviceName}", method=RequestMethod.GET)

public String getServiceInfoByserviceName(@PathVariable("serviceName") String serviceName) ;

}

另外一种就是在configuration中配置feign的契约模式

eg:

@Configuration
public class FeignConfiguration {
//Contract feign的默认契约
@Bean
public Contract feignContract() {
return Contract.Default();
}

}

这样就可以正常创建启动了。

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
当使用Feign进行远程调用时,有时会遇到"Request method 'POST' not supported"的错误提示。这个错误表明Feign不支持该方法的POST请求。为了解决这个问题,可以尝试以下方法: 1. 在Feign接口的方法上加上@RequestParam注解,强制将该方法调用为GET请求。这样可以将POST请求转换为GET请求。 2. 如果出现POST请求的方法不支持GET请求的错误,可以在Feign接口的方法上加上@RequestBody注解,强制将该方法调用为POST请求。 这些方法可以解决Feign中"Request method 'POST' not supported"的错误,使远程调用能够正常进行。引用<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [避坑 | 调用feign远程get请求的接口却提示“Request methodPOST‘ not supported”](https://blog.csdn.net/m0_58071091/article/details/123727009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [spring cloud feign不支持@RequestBody+ RequestMethod.GET报错的解决方法](https://download.csdn.net/download/weixin_38748769/12764348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

huaseven0703

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值