未找到处理程序方法

Posted

技术标签:

【中文标题】未找到处理程序方法【英文标题】:Did not find handler method 【发布时间】:2016-12-13 12:51:50 【问题描述】:

我正在尝试调用一个休息端点,但是当我调用它时得到这个信息,导致 UI 中出现 404 错误

2016-08-07 13:43:42.611 DEBUG 27834 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /api/v1/operations
2016-08-07 13:43:42.614 DEBUG 27834 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/api/v1/operations]
2016-08-07 13:43:42.615 DEBUG 27834 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/api/v1/operations] are [/**]
2016-08-07 13:43:42.616 DEBUG 27834 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/api/v1/operations] are 
2016-08-07 13:43:42.617 DEBUG 27834 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapping [/api/v1/operations] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@39ead1b7]]] and 1 interceptor

同一个包中有2个不同端点的控制器类

@RestController
public class OperationRetrievalController 

    @Autowired
    OperationRetrievalManager operationRetrievalManager;

    private static Logger logger = Logger.getLogger(OperationRetrievalController.class);

    @RequestMapping("/api/v1/operations")
    public ResponseEntity<List<OperationView>> requestUserOperations() 
        String ssoId = "xxxxxx";
        logger.info("Inside request operationRetrivel Manager +++++++++>>>>>>>>");
        return new ResponseEntity<List<OperationView>>(operationRetrievalManager.retrieveOperations(ssoId), HttpStatus.OK);
    



我在同一个包中有另一个类:

@RestController
public class ComponentRetrievalController 

    @Autowired
    ComponentRetrievalManager componentRetrievalManager;

    @RequestMapping(value = "api/v1/component/sso_id" , method=RequestMethod.GET)
    public ResponseEntity<List<Component>> requestUserComponent(@PathVariable("sso_id") String ssoId) 

        return new ResponseEntity<List<Component>>(componentRetrievalManager.retrieveComponents(ssoId), HttpStatus.OK);
    



这是 Spring Boot 应用程序类:

@SpringBootApplication
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages="com.ge.power.bis.repositories")
@ComponentScan(basePackages="com.ge.power.bis.managers")
public class Application 

    private static Logger logger = Logger.getLogger(Application.class);


    public static void main(String[] args) 


        SpringApplication springApplication = new SpringApplication(Application.class);

        ApplicationContext ctx = springApplication.run(args);

        logger.info("Let's inspect the beans provided by Spring Boot:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames)
        
            logger.info(beanName);
               
    

这是我的包的结构

当我删除 Application.java 中的所有注释并只保留 @SpringBootApplication 时,它会出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'componentRetrievalController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.ge.power.bis.managers.ComponentRetrievalManager com.ge.power.bis.controllers.ComponentRetrievalController.componentRetrievalManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.ge.power.bis.managers.ComponentRetrievalManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapable

【问题讨论】:

你尝试调用的 url 是什么 localhost:8080/api/v1/operations 在本地作为 Spring Boot 应用程序运行时 ComponentRetrievalController 定义在哪个包中?它应该在com.ge.power.bis.managers 或其子包之一中。 我已将注释更改为 @ComponentScan(basePackages="com.ge.power.bis.* ") 但收到此错误:ans.factory.BeanCreationException: Error Creating bean with name 'componentRetrievalController' : 自动装配依赖注入失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:com.ge.power.bis.managers.ComponentRetrievalManager com.ge.power.bis.controllers.ComponentRetrievalController.componentRetrievalManager;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of typ 【参考方案1】:

您需要将服务类标记为“可用于制作 bean”,以便 Spring 的组件扫描能够制作 bean 并注入值。

这可以通过@Component注解来完成。正如@sromit 的回答中提到的,添加@Service 注释也可以,因为它已经继承了@Component@Repository@Controller 也继承了 @Component 注解。

【讨论】:

【参考方案2】:

Impl 类之一缺少@Service

【讨论】:

以上是关于未找到处理程序方法的主要内容,如果未能解决你的问题,请参考以下文章

NodeJS 未退出,如何找到打开的处理程序?

在 micronaut 中覆盖未找到的异常处理程序

excel2013使用vba代码提示未找到提供程序,改程序可能未正确安装怎么处理

IIS Express错误http 404.17 - 未找到运行WCF服务

ADODB.Connection 错误 '800a0e7a',未找到提供程序。该程序可能未正确安装

WildFly 9.0.1 WFLYCTL0158 处理程序“控制台”未找到