Spring Data Rest自定义控制器 [英] Spring Data Rest Custom Controller

查看:193
本文介绍了Spring Data Rest自定义控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要使用自定义控制器覆盖其余资源中删除功能的地方.这是restResource的代码

I have requirement where in which i need to override the delete functionality from the rest Resource using a custom controller.here is the code for restResource

@RepositoryRestResource
    public interface SampleRepository extends JpaRepository<Sample,Long>{
List<Sample> findBySampleNumber(@Param("sampleNumber") String sampleNumber);
    }

我创建了一个自定义控制器,该控制器仅覆盖删除功能

i have created a a custom controller which overides only delete fuctionality

@RepositoryRestController
@RequestMapping("/api/samples")
public class SampleController{
    @Autowired
    SampleRepository sampleRepository;

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable Long id) {
        //do some custom logic here
        //then delete the sample
        //sampleRepository.delete(id);

    }

但是,如果现在尝试制作GET api/samples/1(someId)或在RepositoryRestResource上查找某些搜索功能,我会看到以下错误

However if now try to make a GET api/samples/1(someId) or look up some search functionality on the RepositoryRestResource, I see the following error

"description": "Request method 'GET' not supported"

有一种方法可以仅覆盖一个HTTP动词,而其余的功能都来自存储库.

is there way to override only one HTTP verb have the rest of the functionality coming up from the repository.

但是,如果我从控制器中评论public void delete,我就可以访问所有的crud和Search操作

However if i comment public void delete from the controller i am able to access all the crud and Search operations

有人遇到过这样的问题

我正在使用SPRING_DATA_REST-2.5.1-Release

推荐答案

您需要将控制器定义为

@RepositoryRestController
public class SampleController{
    @Autowired
    SampleRepository sampleRepository;

    @RequestMapping(value = "/api/samples/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable Long id) {

    }

spring数据还提供了在域创建,保存和删除域之前和之后执行的不同事件.

As well as spring data provide different events to perform before and after the domain create,save and delete.

引用 http://docs.spring. io/spring-data/rest/docs/current/reference/html/#events

这篇关于Spring Data Rest自定义控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆