Spring Boot应用程序问题

huangapple go评论62阅读模式
英文:

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(&quot;/api&quot;)

It's also Autowired repository class. http://127.0.0.1:8080/api shows whitelabel error page. Any pointers?

@RestController
@RequestMapping(&quot;/api&quot;)
public class AppController {
	
	@Autowired
	AppRepository appRepository;

	
	@PostMapping(&quot;/order&quot;)
	public ResponseEntity&lt;AppEntity&gt; createOrder(@RequestBody AppEntity order) {

	@GetMapping(&quot;/orders&quot;)
	public ResponseEntity&lt;List&lt;AppEntity&gt;&gt; getAllOrder(@RequestParam(required = false) String name) {

	@GetMapping(&quot;/order/{id}&quot;)
	public ResponseEntity&lt;AppEntity&gt; getOrderById(@PathVariable(&quot;id&quot;) long id) {

Repository class

@Repository
public interface AppRepository extends JpaRepository&lt;AppEntity, Long&gt;{
	
	List&lt;AppEntity&gt; findByName(String name);

}

答案1

得分: 0

检查应用程序属性文件中是否设置了任何上下文路径 "server.servlet.contextPath=/"。

英文:

Check if any context path "server.servlet.contextPath=/" set in the application properties file.

huangapple
  • 本文由 发表于 2020年9月21日 05:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63983851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定