英文:
Spring Boot Application Issues
问题
我正在开发一个 Spring Boot/PostgreSQL 应用程序。以下是项目结构。
src/main/java
com.test.app
com.test.app.controller
com.test.app.model
com.test.app.repository
src/test/java
com.test.app
Eclipse 抛出了一个编译错误,指出“没有符合条件的类型为....repository”的 bean。我不得不在主类中包含以下注解。这里有什么问题?
@ComponentScan(basePackages = {"com.test.app.repository.AppRepository"})
另外,映射对我不起作用。我在控制器类中有以下注解。
@RestController
@RequestMapping("/api")
它还自动装配了 repository 类。http://127.0.0.1:8080/api 显示了白标签错误页面。有什么指示吗?
@RestController
@RequestMapping("/api")
public class AppController {
@Autowired
AppRepository appRepository;
@PostMapping("/order")
public ResponseEntity<AppEntity> createOrder(@RequestBody AppEntity order) {
@GetMapping("/orders")
public ResponseEntity<List<AppEntity>> getAllOrder(@RequestParam(required = false) String name) {
@GetMapping("/order/{id}")
public ResponseEntity<AppEntity> getOrderById(@PathVariable("id") long id) {
Repository 类
@Repository
public interface AppRepository extends JpaRepository<AppEntity, Long> {
List<AppEntity> findByName(String name);
}
英文:
I am developing a spring boot/postgres application. Here's the project structure.
src/main/java
com.test.app
com.test.app.controller
com.test.app.model
com.test.app.repository
src/test/java
com.test.app
Eclipse threw a compilation error saying that "no qualifying bean of type ....repository". I had to include below annotation in the main class. What is wrong here?
@ComponentScan(basePackages = {"com.test.app.repository.AppRepository"})
Also, the mapping doesn't work for me. I have below annotations in the controller class.
@RestController
@RequestMapping("/api")
It's also Autowired repository class. http://127.0.0.1:8080/api shows whitelabel error page. Any pointers?
@RestController
@RequestMapping("/api")
public class AppController {
@Autowired
AppRepository appRepository;
@PostMapping("/order")
public ResponseEntity<AppEntity> createOrder(@RequestBody AppEntity order) {
@GetMapping("/orders")
public ResponseEntity<List<AppEntity>> getAllOrder(@RequestParam(required = false) String name) {
@GetMapping("/order/{id}")
public ResponseEntity<AppEntity> getOrderById(@PathVariable("id") long id) {
Repository class
@Repository
public interface AppRepository extends JpaRepository<AppEntity, Long>{
List<AppEntity> findByName(String name);
}
答案1
得分: 0
检查应用程序属性文件中是否设置了任何上下文路径 "server.servlet.contextPath=/"。
英文:
Check if any context path "server.servlet.contextPath=/" set in the application properties file.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论