Spring Data REST入门(三):自定义配置

Spring Data REST入门(一):两行代码搞定RESTFul
Spring Data REST入门(二):环境搭建+实战演练
一、基础配置
Spring Data REST的基础配置定义在RepositoryRestConfiguration(org.springframework.data.rest.core.config.RepositoryRestConfiguration)类中。

可以通过继承
@Component
public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {

@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
configuration.setBasePath(“/api”)
}
}
或者

@Configuration
class CustomRestMvcConfiguration {

@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {

return new RepositoryRestConfigurerAdapter() {

  @Override
  public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
    configuration.setBasePath("/api")
  }
};

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
这两种方式来配置相应的信息。如果使用的是Spring Boot,则可以在application.properties中直接进行配置。

spring.data.rest.basePath=/api
1
这里只配置了basePath,其他配置同理
二、自定义输出字段

1、隐藏某个字段

public class User {

/**
 * 指定id为主键,并设置为自增长
 */
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@GenericGenerator(name = "increment", strategy = "increment")
private long id;
private String name;
@JsonIgnore
private String password;
private int age;
private boolean sex;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
比如在实体对象User中,我们不希望password 序列化未JSON,在上篇博客中说到,Spring Data REST默认使用的是JackSon,则我们就可以使用在需要隐藏的字段添加@JsonIgnore即可
2、@Projections

@Projection(name=”list”,types=User.class)
public interface ListUser {
String getName();
long getId();
}
1
2
3
4
5
也可以通过@Projection注解实现1中的效果,
请求URL为:127.0.0.1:8080/user?projection=list
返回数据:

{
“_embedded”: {
“users”: [
{
“name”: “小白鱼”,
“id”: 1,
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user/1
},
“user”: {
“href”: “http://127.0.0.1:8080/user/1{?projection}”,
“templated”: true
}
}
},
{
“name”: “小白”,
“id”: 2,
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user/2
},
“user”: {
“href”: “http://127.0.0.1:8080/user/2{?projection}”,
“templated”: true
}
}
},
{
“name”: “小 鱼 “,
“id”: 3,
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user/3
},
“user”: {
“href”: “http://127.0.0.1:8080/user/3{?projection}”,
“templated”: true
}
}
},
{
“name”: “white yu”,
“id”: 4,
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user/4
},
“user”: {
“href”: “http://127.0.0.1:8080/user/4{?projection}”,
“templated”: true
}
}
}
]
},
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user
},
“profile”: {
“href”: “http://127.0.0.1:8080/profile/user
}
},
“page”: {
“size”: 20,
“totalElements”: 4,
“totalPages”: 1,
“number”: 0
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@Projection还可以用来建立虚拟列

@Projection(name=”virtual”,types=User.class)
public interface VirtualUser {

@Value("#{target.name} #{target.age}") 
String getFullInfo();

}
1
2
3
4
5
6
7
这里把User中的name和age合并成一列,这里需要注意String getFullInfo();方法名前面一定要加get,不然无法序列化为JSON数据
url:http://127.0.0.1:8080/user?projection=virtual
返回数据:

{
“_embedded”: {
“users”: [
{
“fullUser”: “小白鱼 25”,
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user/1
},
“user”: {
“href”: “http://127.0.0.1:8080/user/1{?projection}”,
“templated”: true
}
}
},
{
“fullUser”: “小白鱼 25”,
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user/2
},
“user”: {
“href”: “http://127.0.0.1:8080/user/2{?projection}”,
“templated”: true
}
}
}
]
},
“_links”: {
“self”: {
“href”: “http://127.0.0.1:8080/user
},
“profile”: {
“href”: “http://127.0.0.1:8080/profile/user
}
},
“page”: {
“size”: 20,
“totalElements”: 2,
“totalPages”: 1,
“number”: 0
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Projection定义的数据格式还可以直接配置到Repository之上,就像下面代码中的这样

@RepositoryRestResource(path=”user”,excerptProjection=ListUser.class)
public interface UserRepository extends JpaRepository

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值