@Repository 和@RestController 在一起

@Repository and @RestController together

我有一个工作的 JpaRepository 作为接口,还有一个工作的 Restcontroller。

目前是两种类型,我想把它们合二为一!

这是我的:

@Repository
@RestController("problem")
public interface ProblemsController extends JpaRepository<Problem, Integer> {

  @RequestMapping(value = "all", method = RequestMethod.GET)
  List<Problem> findAll();
}

但是当我打电话给 http://localhost:8080/application/problem/all 时,我收到了 404。

怎么办?

尝试将 / 添加到您的 RequestMapping 而不是 @RestController

 @Repository
 @RestController
 public interface ProblemsController extends JpaRepository<Problem, 
 Integer> {

  @RequestMapping(value = "/problem/all", method = RequestMethod.GET)
  List<Problem> findAll();
 }

或者你可以试试这个:

    @Repository
    @RestController
    @RequestMapping("/problem")
    public interface ProblemsController extends JpaRepository<Problem,
                Integer> {

            @RequestMapping(value = "/all", method = RequestMethod.GET)
            List<Problem> findAll();
     }

您可以使用这种方式..但我没有得到您期望的结果。

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

    List<Person> findByLastName(@Param("name") String name);

}

这是一个存储库和 REST 网络服务。

如果您遇到错误。您可以检查 UI 页面是否正确定位此接口或 Not