【问题标题】:@Repository and @RestController together [closed]@Repository 和 @RestController 一起[关闭]
【发布时间】:2017-08-10 09:50:46
【问题描述】:

我有一个可用的 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(value = "/all", method = RequestMethod.GET)
  • 我也认为是一个错字,但你写的是应用程序而不是应用程序
  • @Peter Rader 看看我的解决方案
  • FWIW JPARepository != JPA API。标签固定
  • 我投票决定将此问题作为离题结束,因为它是一个 XY 问题并且会促进不良架构。

标签: java spring spring-data-jpa restful-url


【解决方案1】:

你可以使用这种方式..但我没有得到你所期望的。

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 Web 服务。

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

【讨论】:

  • 如果有帮助,您可以选择答案。点击下方的小勾号。
【解决方案2】:

尝试将 / 添加到您的 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();
     }

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 2016-12-26
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多