我在使用不同的Spring Controller时遇到问题。
我们正在使用标有PagingAndSortingRepository
的标准Spring RepositoryRestResource
来提供搜索响应。一种方法是
Page<Custodian> findByShortNameContainingIgnoreCase(@Param("shortName") String shortName, Pageable p);
它返回Custodian
中满足Pages中分组条件的所有实体。
结果如下:
{
"_embedded" : {
"custodians" : [ {
"shortName" : "fr-custodian",
"name" : "french custodian",
"contact" : "Francoir",
"_links" : {
"self" : {
"href" : "http://127.0.0.1:9000/api/custodians/10004"
},
"custodian" : {
"href" : "http://127.0.0.1:9000/api/custodians/10004"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://127.0.0.1:9000/api/custodians/search/findByShortNameContainingIgnoreCase?shortName=fr&page=0&size=3&sort=shortName,asc"
}
},
"page" : {
"size" : 3,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
这是我们的前端期望的格式。
但是,我们需要另一个查询,该查询导致一个很长的函数(因此还有URL),因为它需要多个参数。
具体而言,它在Custodian
中全局搜索字符串。因此,每个参数都具有相同的值。
为了缩短URL,我们创建了一个用RepositoryRestController
注释的ResponseBody
,并实现了一个仅接受一个参数,内部调用长URL并返回结果的函数({{1 }}。
Page
不幸的是,Spring没有将相同的格式应用于我们的函数结果。
它看起来像这样:
@RequestMapping(value = "/custodian", method = RequestMethod.GET)
public Page<Custodian> search(@RequestParam(value = "keyWord") String keyWord, Pageable p) {
return repo.LONGURL(keyWord, keyWord, p);
}
如何让Spring以我们的自定义方法提供相同格式?