通过简单的增删改查知道了spring data rest 的基本语法,那么带入一些真实环境来实现一些用例。
一、密码一栏的信息不想让用户看到。
在user实体类中增加
@JsonIgnore
private String password;
http://localhost:8081/api/user/2
返回结果:
{
"name": "李四",
"age": 25,
"sex": "男",
"datatime": "2020-02-20 01:52:26",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/2"
},
"user": {
"href": "http://localhost:8081/api/user/2"
}
}
}
二、组合输出用户姓名、性别、年龄。
新增一个接口:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.rest.core.config.Projection;
import test.entity.User;
@Projection(name="list",types=User.class)
public interface userList {
@Value("#{target.name},年龄:#{target.age},性别:#{target.sex}")
public String getFullInfo();
}
输入 http://localhost:8081/api/user?projection=list
或
将该接口配置在UserRepository上。
package test.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import test.entity.User;
/**
* path="user": 映射路由
* excerptProjection 映射接口
* User:实体类
* Long:主键
* @author white
*
*/
@RepositoryRestResource(path="user",excerptProjection=userList.class)
public interface UserRepository extends JpaRepository<User, Integer>{
}
输入http://localhost:8081/api/user
返回内容:(如果输入http://localhost:8081/api/user/2,显示不出这个组合字段)
{
"_embedded": {
"users": [
{
"fullInfo": "张三,年龄:16,性别:男",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/1"
},
"user": {
"href": "http://localhost:8081/api/user/1{?projection}",
"templated": true
}
}
},
{
"fullInfo": "李四,年龄:25,性别:男",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/2"
},
"user": {
"href": "http://localhost:8081/api/user/2{?projection}",
"templated": true
}
}
},
{
"fullInfo": "王五,年龄:32,性别:女",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/3"
},
"user": {
"href": "http://localhost:8081/api/user/3{?projection}",
"templated": true
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/user{?page,size,sort,projection}",
"templated": true
},
"profile": {
"href": "http://localhost:8081/api/profile/user"
}
},
"page": {
"size": 20,
"totalElements": 3,
"totalPages": 1,
"number": 0
}
}
三、查找姓名为张三的信息
在UserRepository配置如下内容:
@RestResource(path="name",rel="name")
public String findByName(@Param("name") int name);
输入http://localhost:8081/api/user/search/name?name=张三
返回结果:
{
"name": "张三",
"age": 16,
"sex": "男",
"datatime": "2020-02-03 01:52:14",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/1"
},
"user": {
"href": "http://localhost:8081/api/user/1{?projection}",
"templated": true
}
}
}
这里有两个问题:1.为什么请求的url要加上search?2.为什么方法名必须是findByName?
按照Spring Data的规范的规定,查询方法以find | read | get开头(比如 find、findBy、read、readBy、get、getBy),涉及查询条件时,条件的属性用条件关键字连接,要注意的是:条件属性以首字母大写。框架在进行方法名解析时,会先把方法名多余的前缀截取掉,然后对剩下部分进行解析。
jpa命名规范请参考:点击这里。
四、查询一个姓张的信息。
在UserRepository配置如下内容:
@RestResource(path="like",rel="nameStartsWith")
public List<User> findByNameStartsWith(@Param("name") String name);
输入:http://localhost:8081/api/user/search/like?name=张
返回结果:
{
"_embedded": {
"users": [
{
"fullInfo": "张三,年龄:16,性别:男",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/1"
},
"user": {
"href": "http://localhost:8081/api/user/1{?projection}",
"templated": true
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/user/search/like?name=%E5%BC%A0"
}
}
}
五、查询一个姓名为张三,性别为男的信息。
在UserRepository配置如下内容:
@RestResource(path="more",rel="nameAndSex")
public List<User> findByNameAndSex(@Param("name") String name,@Param("sex") String sex);
输入:http://localhost:8081/api/user/search/more?name=张三&sex=男
返回结果:
{
"_embedded": {
"users": [
{
"fullInfo": "张三,年龄:16,性别:男",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/1"
},
"user": {
"href": "http://localhost:8081/api/user/1{?projection}",
"templated": true
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/user/search/more?sex=%E7%94%B7&name=%E5%BC%A0%E4%B8%89"
}
}
}
六、将性别为男的信息按年龄排序输出。
在UserRepository配置如下内容:
@RestResource(path="ageDesc",rel="ageDesc")
public List<User> findBySexOrderByAgeDesc(@Param("sex") String sex);
输入:http://localhost:8081/api/user/search/ageDesc?sex=男
返回结果:
{
"_embedded": {
"users": [
{
"fullInfo": "李四,年龄:25,性别:男",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/2"
},
"user": {
"href": "http://localhost:8081/api/user/2{?projection}",
"templated": true
}
}
},
{
"fullInfo": "张三,年龄:16,性别:男",
"_links": {
"self": {
"href": "http://localhost:8081/api/user/1"
},
"user": {
"href": "http://localhost:8081/api/user/1{?projection}",
"templated": true
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8081/api/user/search/ageDesc?sex=%E7%94%B7&%E5%BC%A0%E4%B8%89"
}
}
}