I have below pom.xml in the SpringBoot application. 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.
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:
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:
Spring Boot Application 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:
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:
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'; nested exception is 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. 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 :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-03-21 09:51:35.312 ERROR 20560 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
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.
The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.repository.MenuRightRepository' in your configuration.
CodePudding user response:
I am sure it is your main class configuration issue. By default, framework will scan beans in same package as of main class and sub-packages. e.g. below will not work
com/example/controller/FooController.java
com/example/main/MainClass.java
as applciation will scan for beans in com.example.main package and its subpackages only.
If that is case, you will need to update 'scanBasePackages' attribute of @SpringBootApplication annotation.
CodePudding user response:
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