英文:
Dependency injection not working on spring boot app
问题
I didn't figure out why this is not working, I am trying to implement a simple end point.
This is my controller:
@RestController
public class CustomerController {
private final CustomerService customerService;
@Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping("/")
Customer getCustomer() {
return customerService.getCustomer();
}
}
And this is my service:
@Component
public class CustomerService {
public Customer getCustomer() {
return new Customer("Anas", 23, "Monday");
}
}
When I go to localhost:8000/ I got the next error message: "This application has no explicit mapping for /error, so you are seeing this as a fallback."
I tried to add another endpoint like this:
@GetMapping("/hi")
String sayHi() {
return "Hello World";
}
And it's working. I assume that the getCustomer
method on the controller is not working because the object is not injected properly. What am I missing?
英文:
i didn't figure out why this is not working , i am trying to implement a simple end point
this is my controller
@RestController
public class CustomerController {
private final CustomerService customerService;
@Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping("/")
Customer getCustomer(){
return customerService.getCustomer();
}
}
and this is my service
@Component
public class CustomerService {
public Customer getCustomer(){
return new Customer("Anas",23,"Monday");
}
}
when i go to localhost:8000/ i got the next error message : " This application has no explicit mapping for /error, so you are seeing this as a fallback. "
i tried to add another endpoint like this
@GetMapping("/hi")
String sayHi(){
return "Hello World";
}
and its working , i assume that getCustomer method on controller not working because the object not injected well , what am missing ?
答案1
得分: 0
Ok that's was my bad, the problem was on the Customer Class, I should have added setters and getters for the fields.
英文:
Ok that's was my bad , the problem was on the Custumer Class , i should have add setters and getters for the fields
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论