Feign整合报错:java.lang.IllegalStateException: Method has too many Body parameters

feign类

    package com.mayikt.feign;

    import com.mayikt.goods.service.PayContextService;
    import org.springframework.cloud.openfeign.FeignClient;

    @FeignClient("app-mayikt-pay")
    public interface PayContextServiceFeign extends PayContextService {

    }

controller类

package com.mayikt.pay.controller;

import com.alibaba.fastjson.JSONObject;
import com.mayikt.core.base.BaseResponse;
import com.mayikt.feign.PayContextServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class PayController {
    @Autowired
    private PayContextServiceFeign payContextServiceFeign;
    @GetMapping("/toPayHtml")
    public String toPayHtml(String channelId, String payToken) {
        BaseResponse<JSONObject> jsonObjectBaseResponse = payContextServiceFeign.toPayHtml(channelId, payToken);
        JSONObject data = jsonObjectBaseResponse.getData();
        String payHtml = data.getString("payHtml");
        return payHtml;
    }

}

启动类

package com.mayikt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * portalApp启动类
 */
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class PortalApp {
    public static void main(String[] args) {
        SpringApplication.run(PortalApp.class, args);
    }
}

pom文件(依赖已经导入)

启动报错信息!(只有错误提示)

    9609 ERROR [2020-09-13 16:55:37]  Application run failed
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'payController': Unsatisfied dependency expressed through field 'payContextServiceFeign'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mayikt.feign.PayContextServiceFeign': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.mayikt.core.base.BaseResponse com.mayikt.goods.service.PayContextService.toPayHtml(java.lang.String,java.lang.String)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
    	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759)
    	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395)
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255)
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243)
    	at com.mayikt.PortalApp.main(PortalApp.java:16)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.mayikt.feign.PayContextServiceFeign': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.mayikt.core.base.BaseResponse com.mayikt.goods.service.PayContextService.toPayHtml(java.lang.String,java.lang.String)
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:170)
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:101)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1645)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getObjectForBeanInstance(AbstractAutowireCapableBeanFactory.java:1178)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:257)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1325)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1291)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:584)
    	... 19 more
    Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract com.mayikt.core.base.BaseResponse com.mayikt.goods.service.PayContextService.toPayHtml(java.lang.String,java.lang.String)
    	at feign.Util.checkState(Util.java:128)
    	at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:117)
    	at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:133)
    	at feign.Contract$BaseContract.parseAndValidatateMetadata(Contract.java:66)
    	at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:146)
    	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:53)
    	at feign.Feign$Builder.target(Feign.java:218)
    	at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:39)
    	at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:223)
    	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:244)
    	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:163)
    	... 30 more
    Disconnected from the target VM, address: '127.0.0.1:60642', transport: 'socket'

    Process finished with exit code 1

异常信息很清楚,这个方法定义了2个请求体。

public String toPayHtml(String channelId, String payToken)

啊?该怎么解决,大哥

public interface PayContextService {

    @GetMapping("/toPayHtml")
    public BaseResponse<JSONObject> toPayHtml(String channelId, String payToken);
}

这是我支付service的代码

只定义一个请求体。用对象封装参数,。

@Controller
public class PayController {
    @Autowired
    private PayContextServiceFeign payContextServiceFeign;
    @GetMapping("/toPayHtml")
    public String toPayHtml(PayVo payVo) {
        BaseResponse<JSONObject> jsonObjectBaseResponse = payContextServiceFeign.toPayHtml(payVo.getChannelId(), payVo.getPayToken());
        JSONObject data = jsonObjectBaseResponse.getData();
        String payHtml = data.getString("payHtml");
        return payHtml;
    }
}

这个样子吗

是这个 PayContextService。这个被feign代理了。

还是懵的 :joy:

feign那个的参数你加上@RequestParam(name=“”)就行

spring feign接口的基本参数必须使用@PathVariable(name="")标示,不然是找不到对应参数的

1 Like