I am developing an application with spring boot and spring security and jwt but when I run my application I get the following error which I cannot resolve:
java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:807) [spring-boot-2.4.1.jar:2.4.1] at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788) [spring-boot-2.4.1.jar:2.4.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.4.1.jar:2.4.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309) [spring-boot-2.4.1.jar:2.4.1] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298) [spring-boot-2.4.1.jar:2.4.1] at courtjwtudemy.jwtproject.JwtProjectA pplication.main(JwtProjectApplication.java:30) [classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.4.1.jar:2.4.1]
Here is my main spring boot class:
package courtjwtudemy.jwtproject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import courtjwtudemy.jwtproject.dao.MyUserReporisitory;
import courtjwtudemy.jwtproject.dao.ProductReporisitory;
import courtjwtudemy.jwtproject.entity.MyRole;
import courtjwtudemy.jwtproject.entity.Product;
import courtjwtudemy.jwtproject.service.AccountService;
@SpringBootApplication
public class JwtProjectApplication implements CommandLineRunner{
@Autowired
private ProductReporisitory prodRepository;
@Autowired
private MyUserReporisitory myUserReporisitory;
@Autowired
private AccountService accountService;
public static void main(String[] args) {
SpringApplication.run(JwtProjectApplication.class, args);
}
@Bean
BCryptPasswordEncoder getBCPE() {
return new BCryptPasswordEncoder();
}
@Override
public void run(String... args) throws Exception {
// TODO Auto-generated method stub
Product p = new Product();
p.setName("chargeur de soleil");
p.setDescription("hazar");
p.setPrice(45);
p.setPhoto("https://cdn3.lingerie-sipp.com/36357-large_default/debardeur-homme-isolant-innovation-20-noir.jpg");
p.setPromotion(false);
prodRepository.save(p);
// add user
accountService.saveRole(new MyRole(null, "User"));
accountService.saveRole(new MyRole(null, "Admin"));
accountService.saveUser("nawfal", "bgr", "bgr");
accountService.saveUser("admin", "bgr", "bgr");
accountService.saveUser("user", "bgr", "bgr");
myUserReporisitory.findAll().forEach(u -> {
System.out.println("user :" + u.getUsername() + "password :" + u.getPassword());
});
}
}
and here is my entity user class:
package courtjwtudemy.jwtproject.entity;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import com.fasterxml.jackson.annotation.JsonProperty;
@Entity
public class MyUser {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String username;
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<MyRole> roles = new ArrayList<>();
public MyUser() {
super();
}
public MyUser(Long id, String username, String password, Collection<MyRole> roles) {
super();
this.id = id;
this.username = username;
this.password = password;
this.roles = roles;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Collection<MyRole> getRoles() {
return roles;
}
public void setRoles(Collection<MyRole> roles) {
this.roles = roles;
}
}
and here is my entity role class:
package courtjwtudemy.jwtproject.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class MyRole {
@Id
@GeneratedValue (strategy = GenerationType.IDENTITY)
private Long id;
private String roleName;
public MyRole(Long id, String roleName) {
super();
this.id = id;
this.roleName = roleName;
}
public MyRole() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
}
can someone help me please?
You can try moving all the business logic to a new class which is annotated with Controller or Service.
Keep only the public static method in your main class.
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.