Reputation: 160
I am building this Spring Boot application with Hibernate and MySQL. I know this is pretty basic and the same question is asked multiple times but I can't figure out why my controller isn't hitting and gives 404 error. As I see, the problem is with ComponentScan
where my @SpringBootApplication
and @RestController
reside within one package while my @Repository
and @Entity
lie in another package.
When I include the package as @ComponentScan(basePackages = "com.sample.user")
, the project builds and runs successfully but does not hit the GET method getUser()
and no console output of an error as well. The GET method hits only when I omit the
@Autowired private UserRepository userRepository;
from the controller class along with @ComponentScan
in application class.
Controller
package com.sample.rest.controller;
import com.sample.user.entity.User;
import com.sample.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping ("user")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping("/")
public User getUser() {
User user = new User();
user.setFirstName("Lady");
user.setLastName("Gaga");
user.setEmail("[email protected]");
userRepository.save(user);
return user;
}
}
Application
package com.sample.rest;
import com.sample.rest.controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.sample.user")
public class RestServicesApplication {
public static void main(String[] args) {
SpringApplication.run(RestServicesApplication.class, args);
}
}
Repository Interface
package com.sample.user.repository;
import com.sample.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
@EnableJpaRepositories
@Repository
public interface UserRepository extends JpaRepository<User, Long> {}
Entity
package com.sample.user.entity;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
@Entity
@Table(name = "user")
@EntityListeners(AuditingEntityListener.class)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Column(name = "email_address", nullable = false)
private String email;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Browser Page https://ibb.co/KDsqLn3
Upvotes: 0
Views: 1244
Reputation: 673
Use this it will resolve your Issue.
@ComponentScan(basePackages = "com.sample")
or use multiple package sacn somthing like
@ComponentScan({ "com.sample", "com.sample.user" })
Upvotes: 0
Reputation: 36223
With @ComponentScan(basePackages = "com.sample.user")
you are overwriting the default behavior.
So either remove this and put the packages below the package where you have @SpringBootApplication
or add all packages to @ComponentScan
.
I recommend not to use the default Spring Boot behavior.
So remove ComponentScan
an move RestServicesApplication
to the com.sample
package
Upvotes: 2