主要服务类别
@Service
public class MainService {
@Autowired
HouseRepository repository;
@Autowired
HouseDtoConverter converter;
public House addNewHouse(HouseDTO houseDTO) {
House h = converter.convert(houseDTO);
repository.save(h);
return h;``
}
public Iterable<House> getAllHouses() {
return repository.findAll();
}
}
主控制器类
@RestController // This means that this class is a Controller
@RequestMapping("/demo")
public class MainController {
@Autowired
MainService service;
@RequestMapping(value = "/add", method = RequestMethod.POST) // Map ONLY GET Requests
@ResponseBody
public House addNewHouse (@RequestBody HouseDTO houseDTO) {
return service.addNewHouse(houseDTO);
}
@RequestMapping(value="/all", method = RequestMethod.GET)
@ResponseBody
public Iterable<House> getAllHouses() {
// This returns a JSON or XML with the houses
return service.getAllHouses();
}
和堆栈跟踪: 说明:
vikings.service.MainService中的字段存储库需要找不到类型为'vikings.repository.HouseRepository'的bean。
注入点具有以下注释: -@ org.springframework.beans.factory.annotation.Autowired(required = true)
操作:
考虑在您的配置中定义类型为“ vikings.repository.HouseRepository”的bean。
那是我的SpringBootApplication
@Component
@SpringBootApplication
public class SpringBoot11Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot11Application.class, args);
}
}