当前位置: 首页 > 知识库问答 >
问题:

如何解决bean创建过程中的意外异常;嵌套的例外是java。lang.IllegalStateException:方法的主体参数太多

葛鸿轩
2023-03-14

我正在尝试编写一个服务,它将通过eureka服务器绑定到另一个服务,我想使用FaignClient Hystrix,我无法实现GET控制器,我遇到了以下异常:

 java.base@11/jdk.internal.misc.Unsafe.park(Native Method)
 java.base@11/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:234)
 java.base@11/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2123)
 java.base@11/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1182)
 java.base@11/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:899)
 java.base@11/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1054)
 java.base@11/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1114)
 java.base@11/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
 java.base@11/java.lang.Thread.run(Thread.java:834)
2021-10-29 14:15:14.413  INFO 5036 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-10-29 14:15:14.429 ERROR 5036 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'parserFileController': Unsatisfied dependency expressed through field 'parserServiceFeignClient'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'by.iba.hystrix.service.ParserServiceFeignClient': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity by.iba.hystrix.service.ParserServiceFeignClient.getCars(by.iba.hystrix.filter.CarFilter,org.springframework.data.domain.Pageable)
Warnings:
- 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1425) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:755) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:402) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1247) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1236) ~[spring-boot-2.3.12.RELEASE.jar:2.3.12.RELEASE]
    at by.iba.hystrix.HystrixApplication.main(HystrixApplication.java:18) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'by.iba.hystrix.service.ParserServiceFeignClient': Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity by.iba.hystrix.service.ParserServiceFeignClient.getCars(by.iba.hystrix.filter.CarFilter,org.springframework.data.domain.Pageable)
Warnings:
- 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:529) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    ... 21 common frames omitted
Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.http.ResponseEntity by.iba.hystrix.service.ParserServiceFeignClient.getCars(by.iba.hystrix.filter.CarFilter,org.springframework.data.domain.Pageable)
Warnings:
- 
    at feign.Util.checkState(Util.java:129) ~[feign-core-10.12.jar:na]
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:127) ~[feign-core-10.12.jar:na]
    at org.springframework.cloud.openfeign.support.SpringMvcContract.parseAndValidateMetadata(SpringMvcContract.java:207) ~[spring-cloud-openfeign-core-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:62) ~[feign-core-10.12.jar:na]
    at feign.hystrix.HystrixDelegatingContract.parseAndValidateMetadata(HystrixDelegatingContract.java:47) ~[feign-hystrix-10.12.jar:na]
    at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:151) ~[feign-core-10.12.jar:na]
    at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:49) ~[feign-core-10.12.jar:na]
    at feign.hystrix.HystrixFeign$Builder.target(HystrixFeign.java:63) ~[feign-hystrix-10.12.jar:na]
    at org.springframework.cloud.openfeign.HystrixTargeter.targetWithFallback(HystrixTargeter.java:74) ~[spring-cloud-openfeign-core-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:49) ~[spring-cloud-openfeign-core-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:416) ~[spring-cloud-openfeign-core-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:361) ~[spring-cloud-openfeign-core-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.cloud.openfeign.FeignClientsRegistrar.lambda$registerFeignClient$0(FeignClientsRegistrar.java:246) ~[spring-cloud-openfeign-core-2.2.9.RELEASE.jar:2.2.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.obtainFromSupplier(AbstractAutowireCapableBeanFactory.java:1235) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.15.RELEASE.jar:5.2.15.RELEASE]
    ... 29 common frames omitted


Process finished with exit code 1

这是我的控制器在主服务中的外观:

@RestController
@RequestMapping("/cars")
public class CarController {

    private final CarService carService;

    @Autowired
    public CarController(CarService carService) {
        this.carService = carService;
    }

    @GetMapping
    public ResponseEntity<Page<CarDto>> getCars(CarFilter carFilter, Pageable page) {
        Page<CarDto> cars = carService.getCars(page, carFilter);
        return new ResponseEntity<>(cars, HttpStatus.OK);
    }

这就是我的控制器在服务中的样子,在那里我试图实现飞扬客户端:

@RestController
@RequestMapping("/cars")
public class ParserFileController {

    @Autowired
    private ParserServiceFeignClient parserServiceFeignClient;

    @GetMapping
    public ResponseEntity<Page<CarDto>> getCars(CarFilter carFilter, Pageable page) {

        return parserServiceFeignClient.getCars(carFilter, page);
    }

这是我制作FaignClient的界面

@FeignClient(
        name = "PARSER-FILE-SERVICE",
        url = "http://localhost:8080/cars",
        fallback = ParserServiceFallback.class
)
public interface ParserServiceFeignClient {

    @GetMapping()
    ResponseEntity<Page<CarDto>> getCars(CarFilter carFilter, Pageable page);

这是FaignClient的后备类:


@Component
public class ParserServiceFallback implements ParserServiceFeignClient {

    
    @Override
    public ResponseEntity<Page<CarDto>> getCars(CarFilter carFilter, Pageable page) {
        System.out.println("In fallback method");
        return new ResponseEntity<>(new RestPageImpl<>(), HttpStatus.OK);
    }

CarFilter:

public class CarFilter {

    private String mark;

    private Integer year;

    private String color;

    private String country;

    public CarFilter() {
    }

// getters and setters

请帮帮我,我已经被这个任务折磨了很长时间了。我试图通过POST来实现,但这不适合通过RESTapi获取资源

共有2个答案

长孙玉泽
2023-03-14

GET请求没有正文,请尝试使用其他映射方法,如post。nad尝试通过@RequestParam或@RequestBody等注释以更具体的方式接收对象。

@PostMapping("")
public ResponseEntity<> getCars(@RequestBody CarFilter car, ....){}
廖琪
2023-03-14

GET请求通常没有主体,因此必须传递参数,例如作为查询参数。

尝试使用请求参数注释:

@GetMapping
public ResponseEntity<Page<CarDto>> getCars(@RequestParam("car-filter") CarFilter carFilter, @RequestParam("page") Pageable page)
  
 类似资料:
  • 下面的示例演示了使用OpenFeign时遇到的问题。当响应对象的字段太多时,问题就会变得明显,这会引发一个错误:方法的参数太多。示例1工作正常,但示例2失败。 示例1使用HTTP. POST与示例2中使用的响应对象相同,后者使用HTTP. GET。 为什么在HTTP. GET方法中OpenFaigns限制字段,并抛出异常?我不能使用HTTP. POST获取/获取/读取资源。糟糕的REST API设

  • 我有两个项目共享同一个aidl文件。 在第一个项目中,构建在Eclipse和Android Studio中运行到完成。 如何确定问题????这是什么原因造成的?我正在使用com.google.android.vending.licensing。这可能是相关的吗? 我曾经看到过一个库被包含两次(不同版本)的问题,但这似乎不是这里的问题。任何想法都将非常感谢!

  • 当我使应用程序使用许多库时。我通常遇到的错误是 此错误有一些原因,因为: 数函数 关于第二种情况,很难检测嵌套了什么库。 如果你对这个问题有经验,请分享给我。 更新:

  • 问题内容: 我不明白为什么这几行 导致异常并打印 …而“不可解析的”时间与我传递给SimpleDateFormat()的格式字符串兼容。 问题答案: 这是一个区域设置问题。用:

  • 我在RestController类中有以下requestMethod,它运行良好: 我还有一个假客户也工作得很好。我在两种方法中都添加了一个名为forceSupplier的新参数,但添加后,我发现,但我真的不明白为什么我会收到此消息,因为参数是相同的。 这是假装的方法: 我做错了什么?谢谢

  • 当我尝试使用循环类的Application Context创建bean时,我面临以下错误!Circle类实现了接口,其中包含一个方法。 配置: 我正在学习Spring,并在eclipse中设置了一个Java项目,其中包含所有必需的jar(Spring和Apache公共日志记录)。我在src文件夹的类路径中有spring.xml。我也尝试过下载最新的jar(Spring 4.0.4版本,以前适用于4