简体   繁体   English

无法命中 Rest Controller Spring 开机

[英]Unable to Hit Rest Controller Spring Boot

I have below pom.xml in the SpringBoot application.我在 SpringBoot 应用程序中有以下 pom.xml。 While running the application, I am not facing any errors.运行应用程序时,我没有遇到任何错误。 But when I am hitting the URL through postman or browser, then my java code present inside controller is not getting executed.但是,当我通过 postman 或浏览器访问 URL 时,controller 中的 java 代码不会被执行。

pom.xml: pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>MENU-RIGHT</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
  
  <dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
    </dependency>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
    </dependency>
  </dependencies>

</project>

Controller: Controller:

package com.example.controller;

import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.model.MenuRightModel;
import com.example.service.MenuRightService;

@RestController
public class MenuRightController {

    Logger logger = Logger.getAnonymousLogger();
    @Autowired
    private MenuRightService menuRightService;
    
    @RequestMapping(value  = "/getRightByUser/{id}", method=RequestMethod.GET)
    public MenuRightModel findRightByUser(@PathVariable("id") int id) {
        logger.info("Request Received for User Id : "+id);
        return menuRightService.findRightByUser(id);
        
    }
    @RequestMapping(value  = "/getRightAll", method=RequestMethod.GET)
    public MenuRightModel findAllRight() {
        return new MenuRightModel();
        
    }
}

Project Execution Screenshot:项目执行截图:

在此处输入图像描述

Postman Execution Screenshot: Postman 执行截图: 在此处输入图像描述 在此处输入图像描述

Spring Boot Application Class: Spring 引导应用程序 Class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
@ComponentScan("src/main/java")
public class MenuRightApplication {

    public static void main(String[] args) {
    
        SpringApplication.run(MenuRightApplication.class, args);
    }
}

Spring Boot Repository Class: Spring 引导存储库 Class:

package com.dbs.repository;

import org.springframework.stereotype.Repository;

import com.example.model.MenuRightModel;
import org.springframework.data.jpa.repository.JpaRepository;

@Repository
public interface MenuRightRepository extends JpaRepository<MenuRightModel, Integer>{
    MenuRightModel findByUserId(int id);
}

After Adding BasePackage as com.example facing below Exception:添加 BasePackage 为 com.example 后面临以下异常:

2022-03-21 09:51:35.169 WARN 20560 --- [ restartedMain] ConfigServletWebServerApplicationContext: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'menuRightController': Unsatisfied dependency expressed through field 'menuRightService'; 2022-03-21 09:51:35.169 WARN 20560 --- [restartedMain] ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常 - 取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“menuRightController”的 bean 时出错:不满意通过字段“menuRightService”表达的依赖性; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'menuRightServiceImpl': Unsatisfied dependency expressed through field 'menuRightRepository';嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'menuRightServiceImpl': Unsatisfied dependency expressed through field 'menuRightRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.repository.MenuRightRepository' available: expected at least 1 bean which qualifies as autowire candidate.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用类型为“com.example.repository.MenuRightRepository”的合格 bean:预计至少有 1 个 bean 有资格作为自动装配候选者。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2022-03-21 09:51:35.172 INFO 20560 --- [ restartedMain] o.apache.catalina.core.StandardService: Stopping service [Tomcat] 2022-03-21 09:51:35.189 INFO 20560 --- [ restartedMain] ConditionEvaluationReportLoggingListener:依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 2022-03-21 09:51:35.172 INFO 20560 --- [restartedMain] o.apache.catalina.core.StandardService:停止服务 [Tomcat] 2022-03-21 09:51:35.189 INFO 20560 --- [restartedMain] ConditionEvaluationReportLoggingListener:

Error starting ApplicationContext.启动 ApplicationContext 时出错。 To display the conditions report re-run your application with 'debug' enabled.要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2022-03-21 09:51:35.312 ERROR 20560 --- [ restartedMain] osbdLoggingFailureAnalysisReporter: 2022-03-21 09:51:35.312 错误 20560 --- [restartedMain] osbdLoggingFailureAnalysisReporter:


APPLICATION FAILED TO START应用程序启动失败


Description:描述:

Field menuRightRepository in com.example.service.impl.MenuRightServiceImpl required a bean of type 'com.example.repository.MenuRightRepository' that could not be found. com.example.service.impl.MenuRightServiceImpl 中的字段 menuRightRepository 需要找不到类型为“com.example.repository.MenuRightRepository”的 bean。

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)注入点具有以下注释: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:行动:

Consider defining a bean of type 'com.example.repository.MenuRightRepository' in your configuration.考虑在您的配置中定义类型为“com.example.repository.MenuRightRepository”的 bean。

I am sure it is your main class configuration issue.我确定这是您的主要 class 配置问题。 By default, framework will scan beans in same package as of main class and sub-packages.默认情况下,框架将扫描与主包 class 和子包相同的 package 中的 bean。 eg below will not work例如下面将不起作用

com/example/controller/FooController.java com/示例/控制器/FooController.java

com/example/main/MainClass.java com/example/main/MainClass.java

as applciation will scan for beans in com.example.main package and its subpackages only.因为 applciation 将扫描 com.example.main package 及其子包中的 bean。

If that is case, you will need to update 'scanBasePackages' attribute of @SpringBootApplication annotation.如果是这种情况,您将需要更新 @SpringBootApplication 注释的“scanBasePackages”属性。

You need to add a package name in the component scan, in your case, it is @ComponentScan("com.example.controller"), Standard way to create other packages is under the main root package您需要在组件扫描中添加一个 package 名称,在您的情况下,它是 @ComponentScan("com.example.controller"),创建其他包的标准方法是在主根 package 下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM