Issue
I'm new on java world.I have this error "package org.springframework.boot does not exist" on my SpringApplication Runner class.But i added Spring Boot dependency to pom.xml I do not understand why i got this error.
pom.xml
<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-jersey</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-web</artifactId>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>javax.transaction-api</artifactId>
<version>1.2</version>
</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>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
UserDao
@Repository
public class UsersDao {
@PersistenceContext
private EntityManager entityManager;
public void save(Users users){
entityManager.persist(users);
}
}
UserDTO
public class UsersDto {
private Integer id;
private String name;
private String surname;
private String username;
private Boolean isSuperUser;
private String email;
private String password;
private Date created_at;
private Date updated_at;
public UsersDto(){
}
public UsersDto(Users users){
this.id = users.getId();
this.name = users.getName();
this.surname = users.getSurname();
this.email = users.getEmail();
this.isSuperUser = users.getSuperUser();
this.username = users.getUsername();
this.password = users.getPassword();
}//AND GETTER&SETTER
UserResources
@Component
@Path("/users")
public class UsersResources {
@Autowired
UsersService usersService;
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response saveCity(UsersDto usersDto){
Users users;
try{
users = usersService.saveUsers(usersDto);
}catch (Exception e){
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return Response.ok(users).build();
}
}
And UserServices
@Service
public class UsersService {
@Autowired
private UsersDao usersDao;
@Transactional
public Users saveUsers(UsersDto usersDto){
Users users = new Users();
users.setName(usersDto.getName());
users.setEmail(usersDto.getEmail());
users.setSuperUser(usersDto.getSuperUser());
users.setSurname(usersDto.getSurname());
users.setPassword(usersDto.getPassword());
usersDao.save(users);
return users;
}
}
User model
@Entity
@Table(name="users")
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="is_super_user")
private Boolean isSuperUser;
@Column(name="email")
private String email;
@Column(name="created_at")
private Date created_at;
@Column(name="updated_at")
private Date updated_at;
@PrePersist
protected void onCreate() {
created_at = new Date();
}
@PreUpdate
protected void onUpdate() {
updated_at = new Date();
}
Where is my fault?How can i fix it?And I want to ask similar question.Sometimes we got some errors but i can't find it.So we understand this error's reason is pom.xml dependency conflicts.But no error on maven.We are tired because of incompatible dependency.Has it any solution of prevent dependency conflicts?(like maven dependency validator)
Solution
For using spring boot a easier way it's to create a Spring Boot project using Spring Initiliazr https://start.spring.io/. You can create from here a maven project that has the Spring Boot or more other dependencies alredeay patched in.
Answered By - Dina Bogdan
Answer Checked By - Terry (JavaFixing Volunteer)