spring data rest 定制访问路径
**********************
示例
*******************
pojo 层
Person
@Data
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private Integer age;
}
*******************
dao 层
PersonRepository
@RepositoryRestResource(path = "/person")
public interface PersonRepository extends JpaRepository<Person,Integer> {
@RestResource(path = "/findByAgeBetween",rel = "findByAgeBetween")
List<Person> findByAgeBetween(@Param("min") Integer min, @Param("max") Integer max);
}
personRepository 访问路径 /person
findByAgeBetween 访问路径 /findByAgeBetween
**********************
使用测试
localhost:8080/person/1
{
"name": "瓜田李下",
"age": 20,
"_links": {
"self": {
"href": "http://localhost:8080/person/1"
},
"person": {
"href": "http://localhost:8080/person/1"
}
}
}
localhost:8080/person/search/findByAgeBetween?min=20&max=22
{
"_embedded": {
"persons": [
{
"name": "瓜田李下",
"age": 20,
"_links": {
"self": {
"href": "http://localhost:8080/person/1"
},
"person": {
"href": "http://localhost:8080/person/1"
}
}
},
{
"name": "瓜田李下2",
"age": 21,
"_links": {
"self": {
"href": "http://localhost:8080/person/2"
},
"person": {
"href": "http://localhost:8080/person/2"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/person/search/findByAgeBetween?min=20&max=22"
}
}
}