简体   繁体   中英

Spring Boot keeps getting stuck at startup ( JVM running ) while running application

That's the project structure

I used mysql and created employeemanager table.

That's the content of each file :

EmployeeManagerApplication.java :

package tn.poulina.EmployeeManager;

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

/*@ComponentScan(basePackages={"tn.poulina.EmployeeManager.model"} )*/

@SpringBootApplication

public class EmployeeManagerApplication {

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

}

Employee.java :

package tn.poulina.EmployeeManager.model;

import java.io.Serializable;

import javax.persistence.*;


@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false,updatable = false)
private Long id;
private String name;
private String email;
private String jobTitle;
private String phone;
private String imageUrl;
@Column(nullable = false,updatable = false)
private String employeeCode;

public Employee()
{}

public Employee(String name,String email,String jobTitle ,String phone ,String imageUrl,String 
employeeCode)
{
    this.name=name;
    this.email=email;
    this.jobTitle=jobTitle;
    this.phone=phone;
    this.imageUrl=imageUrl;
    this.employeeCode=employeeCode;
}

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;
}
public String getEmail()
{
    return email;
}

public void setEmail(String email)
{
    this.email=email;
}
public String getJobTitle()
{
    return jobTitle;
}

public void setJobTitle(String jobTitle)
{
    this.jobTitle=jobTitle;
}
public String getPhone()
{
    return phone;
}

public void setPhone(String phone)
{
    this.phone=phone;
}
public String getImageUrl()
{
    return imageUrl;
}

public void setImageUrl(String imageUrl)
{
    this.imageUrl=imageUrl;
}

public String getEmployeeCode()
{
    return employeeCode;
}

public void setEmployeeCode(String employeeCode)
{
    this.employeeCode=employeeCode;
}


@Override
public String toString(){
    return "Employee{"+"id="+id+"/name="+name+"/email="+email+"/jobTitle="+jobTitle+"/phone="+phone+"/imageUrl="+imageUrl+"}";
}

application.properties :

# MySQL Configuration

spring.datasource.url=jdbc:mysql://localhost:3306/employeemanager
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

while running ,it gets stuck in here

i tried typing localhost:8080 on browser and it shows:

i have also tried @ComponentScan(basePackages={"tn.poulina.EmployeeManager.model"} ) in 'EmployeeManagerApplication.java ' and tried moving 'EmployeeManagerApplication.java' to package 'model' but the result is always the same .

From your project structure, you only have model class . You need to add Controller class with handler method that will handle "/" path. To add more:

  • You have model class, you have database properties in application.properties but you don't have repository interface or class to actually connect your application to database to do CRUD operations. for that you have to add spring-boot-starter-data-jpa dependency into your pom.xml.
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

you can then create repository interface as:
    public interface EmployeeRepository extends JpaRepository<Employee,Long> {}

  • Then, you should create service class or service interface and its implementation to get the data you want from database through repository interface to controller bean. you have inject EmployeeRepository into this service class.
    @Service
    public class EmployeeService{
        
        private final EmployeeRepository empRepo;
        
        public EmployeeService(EmployeeRepository empRepo) {
            this.empRepo = empRepo;
        }
        public List<Employee> getAllEmp(){
            return empRepo.findAll();
        }
        //there could many other methods.....
     }

  • Then you can create your RestController class and add handler method for your end points. The following is a sample.
    @RestController
    public class EmployeeController {
        private final EmployeeService empServ;
        public EmployeeController(final EmployeeService empServ){
            this.empServ = empServ;
        }
        @GetMapping("/") 
        public List<Employee> getAllEmployees(){
             return empServ.getAllEmp();
       }
    }

Then you will see something if you have it in your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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