I tried to test Spring Boot with MongoDB but when I run mvn, the server run just fine but can't find the mapping in the RestController
#RestController
#RequestMapping("/mongotest")
public class UserController {
#Autowired
private UserService service;
#RequestMapping(value = "/getallusers" , method = RequestMethod.GET)
public List<User> getAllUser(){
return service.findAll();
}
#RequestMapping(value = "/saveuser" , method = RequestMethod.POST)
public void saveUser(#RequestBody User user){
service.createUser(user);
}
#GetMapping(value = "/hello")
public String test(){
return "hello world";
}
}
#Repository
public interface UserDAO extends MongoRepository<User,String>{
List<User> findByName(String name);
List<User> findByDepartment(String department);
}
#Entity
#Getter #Setter #NoArgsConstructor #ToString #AllArgsConstructor
#Document(collection = "users")
public class User{
#Id
private Long id;
#Field(value = "name")
private String name;
#Field(value = "age")
private Integer age;
#Field(value = "department")
private String department;
}
spring.data.mongodb.database= test_mongo
#SpringBootApplication
#EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Deploy successful");
}
}
Update the service class
#Service
public class UserService{
#Autowired
private UserDAO userDAO;
public User createUser(User u){
return userDAO.save(u);
}
public List<User> findByName(String name){
return userDAO.findByName(name);
}
public List<User> findByDepartment(String department){
return userDAO.findByDepartment(department);
}
public List<User> findAll(){
return userDAO.findAll();
}
}
Update pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The program run just fine but in the log can't find the part mapping with the RequestMapping i have in the UserController
And when i run the mapped link return 404
```Project struct
src
main
java
com
example
demo
controller
UserController.java
demo
DemoApplication.java
entity
User.java
repository
UserDAO.java
service
UserService.java
resources
application.properties
pom.xml
2019-09-09 22:42:05.856 INFO 19036 --- [ main] com.example.demo.demo.DemoApplication : Starting DemoApplication on DESKTOP-UNR6TSG with PID 19036 (E:\demo\target\classes started by MemeLord in E:\demo)
2019-09-09 22:42:05.861 INFO 19036 --- [ main] com.example.demo.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-09-09 22:42:06.782 INFO 19036 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-09-09 22:42:06.784 INFO 19036 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-09-09 22:42:06.806 INFO 19036 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 17ms. Found 0 repository interfaces.
2019-09-09 22:42:07.549 INFO 19036 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-09-09 22:42:07.588 INFO 19036 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-09-09 22:42:07.588 INFO 19036 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-09 22:42:07.707 INFO 19036 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-09-09 22:42:07.708 INFO 19036 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1790 ms
2019-09-09 22:42:08.053 INFO 19036 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-09 22:42:09.060 INFO 19036 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-09-09 22:42:09.138 INFO 19036 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:2}]
to localhost:27017
2019-09-09 22:42:09.144 INFO 19036 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 2, 0]}, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=3475900}
2019-09-09 22:42:09.401 INFO 19036 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-09 22:42:09.404 INFO 19036 --- [ main] com.example.demo.demo.DemoApplication : Started DemoApplication in 4.07 seconds (JVM running for 8.531)
Deploy successful
Your packages have the wrong structure. You have to put them into your second demo, where your DemoApplicationis, not the first as you did. Then Spring will recognize them.
Here you can read about the package structuring in Spring Boot.
Please provide your package structure
You either need to have all your controller as a sub-packages from class where is your main method lives or use #ComponentScan annotation. I prefer the second way and highly recommend it to you
Related
Yo, cant resolve this simply problem in h2-console..
I want just create table via Entity class like "MOvie" or "director" but when i open my h2-console,is not created, pls help
POM
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>sk.wynny</groupId>
<artifactId>springLearn</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springLearn</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
app-properties
spring.h2.console.enabled= true
spring.datasource.url=jdbc:h2:mem:wynny
spring.datasource.username=root
MOVIE CLASS
package sk.wynny.modul;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Movie {
#Id
#GeneratedValue
private Long id;
private String name;
public Movie() {
}
public Movie(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
LOGs
2022-02-13 20:11:37.031 INFO 5856 --- [ main] s.w.springLearn.SpringLearnApplication : Starting SpringLearnApplication using Java 17.0.1 on DESKTOP-K3O8I67 with PID 5856 (C:\Users\Patrik Severín\IdeaProjects\springLearn\target\classes started by Wynny in C:\Users\Patrik Severín\IdeaProjects\springLearn)
2022-02-13 20:11:37.034 INFO 5856 --- [ main] s.w.springLearn.SpringLearnApplication : No active profile set, falling back to default profiles: default
2022-02-13 20:11:37.638 INFO 5856 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-02-13 20:11:37.653 INFO 5856 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 5 ms. Found 0 JPA repository interfaces.
2022-02-13 20:11:38.249 INFO 5856 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-02-13 20:11:38.260 INFO 5856 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-02-13 20:11:38.260 INFO 5856 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-13 20:11:38.362 INFO 5856 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-02-13 20:11:38.363 INFO 5856 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1281 ms
2022-02-13 20:11:38.398 INFO 5856 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-02-13 20:11:38.553 INFO 5856 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-02-13 20:11:38.563 INFO 5856 --- [ main] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:wynny'
2022-02-13 20:11:38.735 INFO 5856 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-02-13 20:11:38.789 INFO 5856 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.4.Final
2022-02-13 20:11:38.949 INFO 5856 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-02-13 20:11:39.073 INFO 5856 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2022-02-13 20:11:39.263 INFO 5856 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-02-13 20:11:39.273 INFO 5856 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-02-13 20:11:39.321 WARN 5856 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-02-13 20:11:39.602 WARN 5856 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2022-02-13 20:11:39.734 INFO 5856 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-02-13 20:11:39.746 INFO 5856 --- [ main] s.w.springLearn.SpringLearnApplication : Started SpringLearnApplication in 3.068 seconds (JVM running for 3.417)
i following many videos or manuals and still have same problem, is here some Hero what can resolve it ? :D
Problem was in Thymeleaf engine, if i create spring only with h2,web and jpa is working nicely.Idk why problem is with Thymeleaf..
I am trying to create a Spring Boot API, but I am not getting anything in my logs and U am unable to access using Postman and cURL. Even "hello world" example is also not running.
Changing port did not work as different ports need to be allocated for different services and in my scenario port 8080 is for tomcat and 8081 for mongo-express.
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from com.example.quiz.repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>quiz</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>quiz</name>
<description>quiz project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
QuizApplication.java
package com.example.quiz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#RestController
public class QuizApplication {
public static void main(String[] args) {
SpringApplication.run(QuizApplication.class, args);
}
}
Question.java
package com.example.quiz.model;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
#Data
#Document(collection = "question")
public class Question {
#Id
private String id;
private String questionText;
public Question(
String questionText
) {
this.questionText = questionText;
}
}
QuestionRepository.java
package com.example.quiz.repository;
import com.example.quiz.model.Question;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
#Repository
public interface QuestionRepository extends MongoRepository<Question, String> {
List<Question> findAll();
}
QuizController.java
package com.example.quiz.controller;
import com.example.quiz.model.Question;
import com.example.quiz.repository.QuestionRepository;
import com.example.quiz.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#CrossOrigin(origins = "http://localhost:8081")
#RestController
public class QuestionController {
#Autowired
QuestionRepository questionRepository;
#Autowired
QuestionService questionService;
#GetMapping("/question")
public Question setQuestion(Question question) {
return questionRepository.insert(new Question(
question.getQuestionText()
));
}
#GetMapping("/all")
public List<Question> getQuestion() {
return questionService.getAllQuestions();
}
#RequestMapping("/hello")
public String hello() {
return "Hello";
}
}
QuizService.java
package com.example.quiz.service;
import com.example.quiz.model.Question;
import com.example.quiz.repository.QuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
#Service
public class QuestionService {
#Autowired
QuestionRepository questionRepository;
public List<Question> getAllQuestions() {
return questionRepository.findAll();
}
}
application.properties
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=rootuser
spring.data.mongodb.password=rootpass
spring.data.mongodb.database=quizdatabase
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
spring.data.mongodb.auto-index-creation=true
server.error.include-message = always
docker-compose.yaml
version: "3.8"
services:
mongodb:
image: mongo
container_name: mongodb
ports:
- 27017:27017
volumes:
- data:/data
environment:
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=rootpass
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
- ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
- ME_CONFIG_MONGODB_SERVER=mongodb
volumes:
data: {}
networks:
default:
name: mongodb_network
Logs
2021-08-15 18:42:56.878 INFO 99293 --- [ restartedMain] com.example.quiz.QuizApplication : Starting QuizApplication using Java 1.8.0_151 on Vishwajeets-MacBook-Pro-2.local with PID 99293 (/Users/vishwajeetkumar/Downloads/quiz3Test/target/classes started by vishwajeetkumar in /Users/vishwajeetkumar/Downloads/quiz3Test)
2021-08-15 18:42:56.898 INFO 99293 --- [ restartedMain] com.example.quiz.QuizApplication : No active profile set, falling back to default profiles: default
2021-08-15 18:42:57.641 INFO 99293 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-08-15 18:42:57.643 INFO 99293 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-08-15 18:43:00.470 INFO 99293 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2021-08-15 18:43:00.546 INFO 99293 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 68 ms. Found 1 MongoDB repository interfaces.
2021-08-15 18:43:06.280 INFO 99293 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-08-15 18:43:06.411 INFO 99293 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-15 18:43:06.429 INFO 99293 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-15 18:43:07.227 INFO 99293 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-08-15 18:43:07.228 INFO 99293 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 9569 ms
2021-08-15 18:43:09.206 INFO 99293 --- [ restartedMain] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-08-15 18:43:09.464 INFO 99293 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:2, serverValue:70}] to localhost:27017
2021-08-15 18:43:09.464 INFO 99293 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:71}] to localhost:27017
2021-08-15 18:43:09.468 INFO 99293 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=93274206}
2021-08-15 18:43:10.444 INFO 99293 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-08-15 18:43:15.615 INFO 99293 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-15 18:43:15.715 INFO 99293 --- [ restartedMain] com.example.quiz.QuizApplication : Started QuizApplication in 24.388 seconds (JVM running for 28.746)
I have a Springboot project I created by copying the libraries from mvn repository, not using SPRING INITLZR.
Anyway Springboot main seems to be working fine.
It's just that my Rest Controller is not displaying the message I want to.
Main.class
#SpringBootApplication
public class MovieInfoServiceApplication {
public static void main(String[] args) throws IOException, GeneralSecurityException, MessagingException {
SpringApplication.run(MovieInfoServiceApplication.class, args);
}
}
RestController.class
#RestController
#RequestMapping("/")
public class MovieServiceRestController {
#GetMapping("/hello")
//#CrossOrigin(origins = "http://localhost:9005")
public String hello() {
return "movie-service-api: hello from MovieServiceRestController";
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.backend</groupId>
<artifactId>movie-info-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>movie-info-service</name>
<description>microservices about movie info</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
This is what I got when I access "localhost:9005/hello"
Springboot logs
2020-10-18 02:18:35.481 INFO 4347 --- [ main] com.gmailapijava.main.GmailAPIJavaMain : No active profile set, falling back to default profiles: default
2020-10-18 02:18:38.779 INFO 4347 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$e708c1d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-10-18 02:18:39.142 INFO 4347 --- [ main] .w.s.a.s.AnnotationActionEndpointMapping : Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
2020-10-18 02:18:40.286 INFO 4347 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9005 (http)
2020-10-18 02:18:40.307 INFO 4347 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-10-18 02:18:40.307 INFO 4347 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-18 02:18:41.175 INFO 4347 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-10-18 02:18:41.176 INFO 4347 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 5020 ms
2020-10-18 02:18:42.329 INFO 4347 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-18 02:18:43.369 INFO 4347 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9005 (http) with context path ''
2020-10-18 02:18:43.391 INFO 4347 --- [ main] com.gmailapijava.main.GmailAPIJavaMain : Started GmailAPIJavaMain in 10.504 seconds (JVM running for 14.339)
2020-10-18 02:18:56.903 INFO 4347 --- [nio-9005-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-10-18 02:18:56.905 INFO 4347 --- [nio-9005-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-10-18 02:18:56.919 INFO 4347 --- [nio-9005-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 14 ms
SpringApplication.run(GmailAPIJavaMain.class, args);
should be
SpringApplication.run(MovieInfoServiceApplication.class, args);
Also check if the controller package is getting scanned.
Try it like this
#RestController
public class MovieServiceRestController {
#GetMapping("/hello")
//#CrossOrigin(origins = "http://localhost:9005")
public String hello() {
return "movie-service-api: hello from MovieServiceRestController";
}
}
Spring doesn't create the table specified in my #Entity class in my MySQL database.
I've tried a lot of solutions given here in StackOverflow, like some changes in the application.properties, changing the dialect, setting up the schema etc. I've checked my database permissions and also the user/password information, and it's all fine.
application.properties:
spring.datasource.url=jdbc:mysql://localhost/MyBank?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>MyBanc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MyBanc</name>
<description>JEE spring boot Web Project by Zied </description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Client.java
package com.example.entities;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
#Entity
public class Client implements Serializable {
#Id #GeneratedValue
private long code;
private String nom;
private String email;
#OneToMany(mappedBy = "client",fetch = FetchType.LAZY)
private Collection<Compte> comptes;
public Client() {
super();
}
public Client(String nom, String email) {
super();
this.nom = nom;
this.email = email;
}
public long getCode() {
return code;
}
public void setCode(long code) {
this.code = code;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Collection<Compte> getComptes() {
return comptes;
}
public void setComptes(Collection<Compte> comptes) {
this.comptes = comptes;
}
}
terminal:
2019-12-16 14:24:38.857 INFO 6812 --- [ restartedMain] com.example.MyBank.MyBankApplication : Starting MyBankApplication on DESKTOP-BLK0G7N with PID 6812 (C:\Users\ziedb\OneDrive\Bureau\aa\MyBank\target\classes started by Zied BenOthman in C:\Users\ziedb\OneDrive\Bureau\aa\MyBank)
2019-12-16 14:24:38.862 INFO 6812 --- [ restartedMain] com.example.MyBank.MyBankApplication : No active profile set, falling back to default profiles: default
2019-12-16 14:24:38.935 INFO 6812 --- [ restartedMain] o.s.b.devtools.restart.ChangeableUrls : The Class-Path manifest attribute in C:\Users\ziedb\.m2\repository\org\glassfish\jaxb\jaxb-runtime\2.3.2\jaxb-runtime-2.3.2.jar referenced one or more files that do not exist: file:/C:/Users/ziedb/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.xml.bind-api-2.3.2.jar,file:/C:/Users/ziedb/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/txw2-2.3.2.jar,file:/C:/Users/ziedb/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/istack-commons-runtime-3.0.8.jar,file:/C:/Users/ziedb/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/stax-ex-1.8.1.jar,file:/C:/Users/ziedb/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/FastInfoset-1.2.16.jar,file:/C:/Users/ziedb/.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.2/jakarta.activation-api-1.2.1.jar
2019-12-16 14:24:38.936 INFO 6812 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-12-16 14:24:38.936 INFO 6812 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-12-16 14:24:39.539 INFO 6812 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2019-12-16 14:24:39.557 INFO 6812 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 10ms. Found 0 JPA repository interfaces.
2019-12-16 14:24:39.809 INFO 6812 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-12-16 14:24:40.083 INFO 6812 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-12-16 14:24:40.091 INFO 6812 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-12-16 14:24:40.091 INFO 6812 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2019-12-16 14:24:40.169 INFO 6812 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-12-16 14:24:40.169 INFO 6812 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1233 ms
2019-12-16 14:24:40.288 INFO 6812 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2019-12-16 14:24:40.346 INFO 6812 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.4.9.Final}
2019-12-16 14:24:40.469 INFO 6812 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-12-16 14:24:40.627 INFO 6812 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-12-16 14:24:40.754 INFO 6812 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-12-16 14:24:40.769 INFO 6812 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-12-16 14:24:40.977 INFO 6812 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2019-12-16 14:24:40.984 INFO 6812 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-12-16 14:24:41.019 INFO 6812 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2019-12-16 14:24:41.072 WARN 6812 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-12-16 14:24:41.236 INFO 6812 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-16 14:24:41.388 WARN 6812 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2019-12-16 14:24:41.550 INFO 6812 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-12-16 14:24:41.553 INFO 6812 --- [ restartedMain] com.example.MyBank.MyBankApplication : Started MyBankApplication in 3.075 seconds (JVM running for 3.971)
Based on the tutorial that I'm following, this setup should result in the creation of the table in the MySQL database, but it doesn't happen.
There are some possible causes for this:
The application.properties file is not in the correct location. It
should be located in src/main/resources
Second problem: The entity class. You should add #Column on top of
the attributes
Move the entity package to become like org.example.entities and run
the app
In spring application you should create one pakcage for the one layer(entity, repository,service ...)
I think you can simplify this to achieve first a JDBC CRUD:
Remove all the unnecessary maven dependencies, only have following to begin with :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- in case you wanna add Junits-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
Then have following in your application.properties
spring.datasource.url=jdbc:mysql://localhost:yourMySQLPort/MyBank?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.hibernate.ddl-auto=create
Take care of auto-generation of id of your entity:
#Entity
public class Client implements Serializable {
#Id #GeneratedValue(strategy = GenerationType.AUTO)
private long code;
...
And to keep things simple initially, maintain the Main class i.e Spring boot starter class annotated with #SpringBootApplication in the same package as your entity Client
I've noticed that you haven't mentioned the MySQL server port in your URL, see if thats something that is not initiating the connection. Once you get this sorted, you can add up the rest of the features in your app (thymeleaf based viewing tech etc)
Hope this helps!
My application runs but nothing about mapping is shown in console. My Application class file is above controller and also added #ComponentScan(basePackages : "com.org.name.controller") to scan for controllers. Still no mapping shows up in console. I have commented out #Autowired in controller class as i was getting error of :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-12 11:05:16.014 ERROR 14104 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userService in com.homzhub.lms.controller.UserController required a bean of type 'com.homzhub.lms.service.UserService' 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.homzhub.lms.service.UserService' in your configuration.
Main class:
package com.homzhub.lms;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan(basePackages = {"com.homzhub.lms.controller"})
//#EnableJpaRepositories("repository")
//#EnableAutoConfiguration
public class LmsApplication{
public static void main(String[] args){
SpringApplication.run(LmsApplication.class, args);
}
}
appointment controller :
package com.homzhub.lms.controller;
import java.security.Principal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.homzhub.lms.entity.Appointment;
import com.homzhub.lms.entity.User;
import com.homzhub.lms.service.AppointmentService;
import com.homzhub.lms.service.UserService;
#Controller
#RequestMapping(path = "/appointment")
public class AppointmentController {
// #Autowired
private AppointmentService appointmentService;
//#Autowired
private UserService userService;
#RequestMapping(value = "/create", method = RequestMethod.GET)
public void createAppointment(Model model) {
Appointment appointment = new Appointment();
model.addAttribute("appointment", appointment);
model.addAttribute("dateString", "");
}
#RequestMapping(value = "/create", method = RequestMethod.POST)
public String createAppointmentPost(#ModelAttribute("appointment") Appointment appointment, #ModelAttribute("dateString") String date, Model model, Principal principal) throws ParseException {
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm");
Date d1 = format1.parse(date);
appointment.setDate(d1);
User user = userService.findByUsername(principal.getName());
appointment.setUser(user);
appointmentService.createAppointment(appointment);
return "redirect:/userFront";
}
}
pom.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!-- <version>1.5.21.RELEASE</version> -->
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.homzhub</groupId>
<artifactId>lms</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>lms</name>
<description>Lead Management System</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Console output. After commenting out #Autowired in controller classes the application runs but no mapping is done :
2019-07-12 12:58:04.638 INFO 18828 --- [ main] com.homzhub.lms.LmsApplication : Starting LmsApplication v0.0.1-SNAPSHOT on rms-Lenovo-ideapad-330-15IKB with PID 18828 (/home/rms/lms/target/lms-0.0.1-SNAPSHOT.jar started by rms in /home/rms/lms)
2019-07-12 12:58:04.644 INFO 18828 --- [ main] com.homzhub.lms.LmsApplication : No active profile set, falling back to default profiles: default
2019-07-12 12:58:05.941 INFO 18828 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-07-12 12:58:06.161 INFO 18828 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 204ms. Found 13 repository interfaces.
2019-07-12 12:58:06.849 INFO 18828 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d931452d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-07-12 12:58:07.301 INFO 18828 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-07-12 12:58:07.350 INFO 18828 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-07-12 12:58:07.350 INFO 18828 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-07-12 12:58:07.484 INFO 18828 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-07-12 12:58:07.484 INFO 18828 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2751 ms
2019-07-12 12:58:07.952 INFO 18828 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-07-12 12:58:08.286 INFO 18828 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-07-12 12:58:08.388 INFO 18828 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-07-12 12:58:08.519 INFO 18828 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-07-12 12:58:08.521 INFO 18828 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-07-12 12:58:08.773 INFO 18828 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-07-12 12:58:09.175 INFO 18828 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2019-07-12 12:58:10.967 INFO 18828 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-07-12 12:58:12.284 INFO 18828 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-07-12 12:58:12.364 WARN 18828 --- [ main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-07-12 12:58:12.923 INFO 18828 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2019-07-12 12:58:13.211 INFO 18828 --- [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: f1e93a8f-d01a-4050-8724-87ff348fab02
2019-07-12 12:58:13.388 INFO 18828 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#1dcca8d3, org.springframework.security.web.context.SecurityContextPersistenceFilter#6daf2337, org.springframework.security.web.header.HeaderWriterFilter#70e3f36f, org.springframework.security.web.csrf.CsrfFilter#3c7cfcbb, org.springframework.security.web.authentication.logout.LogoutFilter#7d755813, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#4e25147a, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter#60e5272, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter#5631962, org.springframework.security.web.authentication.www.BasicAuthenticationFilter#56303475, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#250b236d, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#432034a, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#52a70627, org.springframework.security.web.session.SessionManagementFilter#23e44287, org.springframework.security.web.access.ExceptionTranslationFilter#1bfe3203, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#71870da7]
2019-07-12 12:58:13.517 INFO 18828 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-07-12 12:58:13.520 INFO 18828 --- [ main] com.homzhub.lms.LmsApplication : Started LmsApplication in 9.518 seconds (JVM running for 10.158)
UserService class :
package com.homzhub.lms.service;
import java.util.List;
import java.util.Set;
import org.springframework.stereotype.Service;
import com.homzhub.lms.entity.User;
import com.homzhub.lms.security.UserRole;
//#Service("userDetailsService")
#Service
public interface UserService{
User findByUsername(String username);
User findByEmail(String email);
// User findByPhoneNumber(String phoneNumber);
boolean checkUserExists(String username, String email);
boolean checkUsernameExists(String username);
boolean checkEmailExists(String email);
void save(User user);
User createUser(User user, Set<UserRole> userRoles);
User saveUser(User user);
List<User> findUserList();
void enableUser(String username);
void disableUser(String username);
}
If it isn't already, you need to define UserService as a Component, or more appropriately as a Service. If it already is you have to map it, which is a bit weird considering Spring should do that on its own.
Your are only scanning com.homzhub.lms.controller and UserService is not under ComponentScan.
You need to add service package to ComponentScan
#ComponentScan(basePackages = {"com.homzhub.lms"})
You service is under com.homzhub.lms.service package so you have to add this package to #ComponentScan too, so Spring will scan this package too and pick up the classes marked with stereotypes there :
#SpringBootApplication
#ComponentScan(basePackages = {"com.homzhub.lms.controller, "com.homzhub.lms.service"})
public class LmsApplication{
public static void main(String[] args){
SpringApplication.run(LmsApplication.class, args);
}
}
However I can see that your class annotated with #SpringBootApplication is already above all the packages with your components so you could get rid of #ComponentScan annotation at all. So it will scan nested packages by default.
And also remember about annotating your service classes with Spring stereotypes annotations for example #Service so component scan will be able to pick them up.
Uncomment #Autowired annotation. Put #Service annotation on the implementing class instead of the interface and make sure your implementing class is discover-able via componentScan. Also, as a side note, Spring will scan all subpackages of your main class (class with #SpringBootApplication annotation). So it would be a good idea to have a directory structure like com.homzhub.lms as a root and com.homzhub.lms.controller for controllers com.homzhub.lms.service for service and com.homzhub.lms.service.impl if you want to keep implementations in a different package. If you are following this structure, you won't need componentScan.