向Spring RepositoryRestResource添加自定义方法

问题描述 投票:0回答:1

是否可以将自定义方法添加到Spring @RepositoryRestResource并将其公开为HTTP端点,如下面的示例尝试(但失败)?还是必须将自定义方法放在单独的@Controller中?

@RepositoryRestResource(path="person")
public interface PersonRepository extends CrudRepository<Person,Long> {

    // This works, as it's an integral part of what RepositoryRestResource does:
    // curl localhost:8080/person/search/findByNameStartsWith?prefix=MA
    Iterable<Person> findByNameStartsWith(@Param("prefix") String prefix);

    // This doesn't work:
    // curl -X POST -v localhost:8080/person/3/sendWelcomeLetter
    @RequestMapping(path = "/{id}/sendWelcomeLetter", method = RequestMethod.POST)
    default String sendWelcomeLetter(@PathVariable("id") Long id) {
        return "This is a custom method that would generate and send a letter.";
    }
}
java spring spring-data-rest
1个回答
0
投票

sendWelcomeLetter不是ReST。 ReST中的ST表示“状态转移”,您的呼叫是动态的,没有状态转移。

最小的解决方案可能是添加一个新的可为空的列“ sent_welcome_newsletter”。

  • [null表示欢迎通讯尚未发送。
  • [false表示无法发送欢迎时事通讯(收件箱已满或无效的电子邮件)。
  • [true表示欢迎简讯过去已成功发送。

但是:注意legal right不会被DNSBL阻止。如果不遵守此标准,您的完整公司将成为巨大的法律问题!!!

© www.soinside.com 2019 - 2024. All rights reserved.