我只使用一种方法创建了carService
:
public interface CarService {
String getName();
}
我添加了CarServiceImpl
@Component
public class CarServiceImpl implements CarService {
@Override
public String getName() {
return "Fiat";
}
}
为什么我的方法String getCarServiceIml不起作用,我没有得到命令?
@Controller
@RequestMapping(path="/car")
public class ApiController {
@Autowired
private CarServiceImpl carServiceImpl;
public String getCarServiceImpl() {
return carServiceImpl.getName();
}
}
当我访问localhost:8080/car
时,我收到了Whitelabel错误页面:
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Jan 10 11:20:26 CET 2018 There was an unexpected error (type=Not Found, status=404). No message available
但我期待菲亚特。
答案 0 :(得分:0)
首先,我会为此更改代码:
@Service
public class CarServiceImpl implements CarService {
@Override
public String getName() {
return "Fiat";
}
}
这个控制器:
@Controller
@RequestMapping(path="/car")
public class ApiController {
@Autowired
private CarService carService;
@RequestMapping(method = RequestMethod.GET, path="/name")
public String getCarName() {
return carService.getName();
}
}
如果您的SpringBoot配置没问题,这可能会正常工作,请检查您是否将@ComponentScan放在您的Application主类中,并使用您的基础包。