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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章