Reputation: 425
I am new to Spring Boot and I am just trying to get/fetch data from datasource through this code :
EmployeController.java
@RestController
@RequestMapping("/api")
public class EmployeController {
@Autowired
private EmployeService employeService;
@GetMapping("/employe")
public List<Employe> get(){
return employeService.get();
}
}
Employe.java
@Entity
@Table(name="employe")
public class Employe {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private int id;
@Column
private String name;
@Column
private String gender;
@Column
private String department;
@Column
private Date dob;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
@Override
public String toString() {
return "Employe [id=" + id + ", name=" + name + ", gender=" + gender + ", department=" + department + ", dob="
+ dob + "]";
}
EmployeService.java
public interface EmployeService {
List<Employe> get();
Employe get(int id);
void save(Employe employe);
void delete(int id);
}
EmployeServiceImplement.java
@Service
public class EmployeServiceImplement implements EmployeService {
@Autowired
private EmployeDAO employeDAO;
@Transactional
@Override
public List<Employe> get() {
return employeDAO.get();
}
}
EmployeDAO.java
public interface EmployeDAO {
List<Employe> get();
Employe get(int id);
void save(Employe employe);
void delete(int id);
}
EmployeDAOimplement.java
@Repository
public class EmployeDAOImplement implements EmployeDAO {
@Autowired
private EntityManager entityManager;
@Override
public List<Employe> get() {
Session currentSession = entityManager.unwrap(Session.class);
Query<Employe> query = currentSession.createQuery("from Employe", Employe.class);
List<Employe>list = query.getResultList();
return list;
}
}
I have writen all the configuration related to MySQl database into the application.properties and created a employe table into my database.
As i know that Spring Data JPA is much better way to implement than this but as i m just want to begin with the same ... As running my project as Spring Boot App and then from Postman i am getting the 404 Error like this :
I am unable to understand why this is showing the 404 error and how to solve it..
Upvotes: 0
Views: 1419
Reputation: 54
What is your context path configuration in application.properties? You'll see a property same as this server.servlet.context-path
only if you set it. If you didn't set it, then probably the path http://localhost:8080/api/employe
is correct, in that case I'll suggest checking if your application really running on port 8080
, if some other application runs on port 8080
and your application runs on some other port then you'll surely get a 404 response.
Upvotes: 1
Reputation: 851
You could also do it like this:
Set in your application props file:
server.servlet.context-path=/api
Then remove RequestMapping Annotation:
@RestController
public class EmployeController {
@Autowired
private EmployeService employeService;
@GetMapping("/employe")
public List<Employe> get(){
return employeService.get();
}
}
Then you should be able to retrieve it with requeste URL
Upvotes: 1
Reputation: 5118
The annotation on your controller @RequestMapping("/api")
means the route will be http://localhost:8080/api
but you're trying to invoke http://localhost:8080/api/employe
. I think you should just change it to @RequestMapping("/api/employe")
.
Upvotes: 1
Reputation: 1979
You need to add the name of your artifact (project name) in the request URI. Ex:
http://localhost:8080/my-app/api/employe
If you dont want it in the URI, change the contextPath configuration in your application.properties:
server.contextPath = /
Upvotes: 1