feign远程调⽤⽆法传递对象属性405的问题
⽬录
feign远程调⽤⽆法传递对象属性
get请求405错误
使⽤feign请求远端
客户端
服务端
feign.FeignException$MethodNotAllowed
status405readingxxx#yyy(Integer)
异常信息
详细信息
解决⽅案
feign远程调⽤⽆法传递对象属性
get请求405错误
通过查看feign底层源码,feign底层使⽤的是httpurlconnection的⼯具,⽽进⾏传递body的时候,会调⽤getOutputStrean⽅法,⾥边会判断是否是get请求,如果是get请求,则⾃动转为post请求,⽽远⽅的服务只能够⽀持get请求,因此会报错
使⽤feign请求远端
configuration添加如下配置类
sumer;
llect.Lists;
import feign.Contract;
import feign.MethodMetadata;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.cloud.openfeign.annotation.PathVariableParameterProcessor;
import org.springframework.cloud.openfeign.annotation.RequestHeaderParameterProcessor;
import org.springframework.cloud.openfeign.annotation.RequestParamParameterProcessor;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import t.annotation.Bean;
import t.annotation.Configuration;
import onvert.ConversionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import flect.Method;
import java.util.Enumeration;
import java.util.List;
/**
* @author: zhaoxu
* @date: 2021/4/29 0:20
*/
@Configuration
public class FeignConfiguration implements RequestInterceptor, AnnotatedParameterProcessor {
@Override
public void apply(RequestTemplate template) {
/
/开启线程共享
RequestContextHolder.RequestAttributes(), true);
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestAttributes();
HttpServletRequest request = Request();
// 对消息头进⾏配置
Enumeration<String> headerNames = HeaderNames();
if (headerNames != null) {
while (headerNames.hasMoreElements()) {
String name = Element();
String values = Header(name);
template.header(name, values);
}
}
}
@Resource
private ConversionService feignConversionService;
private static final Class<RequestBody> ANNOTATION = RequestBody.class;
@Override
public Class<? extends Annotation> getAnnotationType() {
return ANNOTATION;
}
@Override
springmvc选择题
public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) {
int paramIndex = ParameterIndex();
MethodMetadata metadata = MethodMetadata();
if (metadata.queryMapIndex() == null) {
metadata.queryMapIndex(paramIndex);
metadata.queryMapEncoded(true);
}
if ("GET".plate().method())) {
return true;
}
return false;
}
@Bean
public Contract feignContract() {
List<AnnotatedParameterProcessor> parameterProcessors = wArrayList();
parameterProcessors.add(new PathVariableParameterProcessor());
parameterProcessors.add(new RequestHeaderParameterProcessor());
parameterProcessors.add(new RequestParamParameterProcessor());
parameterProcessors.add(new FeignConfiguration());
return new SpringMvcContract(parameterProcessors, feignConversionService);
}
}
客户端
服务端
feign.FeignException$MethodNotAllowed
status 405 reading xxx#yyy(Integer)
异常信息
使⽤feign 调⽤异常
feign.FeignException$MethodNotAllowed: status 405 reading ConsumerService#findById(Integer)
详细信息
feign.FeignException$MethodNotAllowed: status 405 reading ConsumerService#findById(Integer)
at Status(FeignException.java:100) ~[feign-core-10.2.3.jar:na]
at Status(FeignException.java:86) ~[feign-core-10.2.3.jar:na]
dec.ErrorDecoder$Default.decode(ErrorDecoder.java:93) ~[feign-core-10.2.3.jar:na]
at uteAndDecode(SynchronousMethodHandler.java:149) ~[feign-core-10.2.3.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:78) ~[feign-core-10.2.3.jar:na]
at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103) ~[feign-core-10.2.3.jar:na]
at com.sun.proxy.$Proxy74.findById(Unknown Source) ~[na:na]
at ller.ConsumerController.findById(ConsumerController.java:32) ~[classes/:na]
flect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at flect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.hod.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.hod.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at
org.springframework.web.hod.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at
org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at
org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.hod.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039) ~[spring-webmvc-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) ~[spring-webmvc-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) ~[spring-webmvc-5.1.9.RELEASE.jar:5.1.9.RELEASE]    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at at.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-
9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) ~[spring-web-5.1.9.RELEASE.jar:5.1.9.RELEASE]    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118) ~[spring-web-
5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.22.jar:9.0.22]
at org.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) [tomcat-embed-core-9.0.22.jar:9.0.22]
at org.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.22.jar:9.0.22]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.22.jar:9.0.22]
at org.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.22.jar:9.0.22]
at org.tor.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.22.jar:9.0.22]
at http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.22.jar:9.0.22]
at AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.22.jar:9.0.22]
at AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:853) [tomcat-embed-core-9.0.22.jar:9.0.22]
at at.util.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1587) [tomcat-embed-core-9.0.22.jar:9.0.22]
at at.util.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.22.jar:9.0.22]
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at at.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.22.jar:9.0.22]    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
解决⽅案
根据异常信息可知, 接⼝ ConsumerService中的findById⽅法调⽤出现异常
feign.FeignException$MethodNotAllowed: status 405 reading ConsumerService#findById(Integer)
定位到该⽅法 , 添加@RequestParam("id")注解为其矫正参数
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。