首页 > 解决方案 > 如何在@RestResource 中将“路径”设为空以使端点仅作为 /search{?name}

问题描述

我在存储库中有一个自定义方法,我想更改 Spring 生成的端点。需要忽略“/search”之后的“路径”

目前下面是暴露的端点(/search/findByName{?name})。

 "customFind": {
            "href": "http://localhost:8080/productType/search/findByName{?name}",
            "templated": true
        }

当前代码:

@RepositoryRestResource(collectionResourceRel = "productType", path = "productType")
public interface ProductTypeRepository extends JpaRepository<ProductType, Long> {

    @RestResource(path ="findByName", rel = "customFind")
    ProductType findByNameIgnoreCase(@Param("name") String productTypeName);

}

需要更改“路径”,以便我想访问我的端点,如下所示。

 "customFind": {
            "href": "http://localhost:8080/productType/search{?name}",
            "templated": true
        }

我试过指定path="",但没有任何效果。

我希望最终的端点像 http://localhost:8080/productType/search?name="wooden"

标签: javaspringspring-data-rest

解决方案


根据 Spring Data REST文档

All query method resources are exposed under the search resource.

因此,如果您想要自定义行为,则需要编写自己的自定义控制器方法。


推荐阅读