Spring Data REST入门(三):自定义配置
来源:互联网 发布:php sqlite3 安装 编辑:程序博客网 时间:2024/06/06 00:01
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”)
}
}
或者
@Configurationclass CustomRestMvcConfiguration { @Bean public RepositoryRestConfigurer repositoryRestConfigurer() { return new RepositoryRestConfigurerAdapter() { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { configuration.setBasePath("/api") } }; }}
这两种方式来配置相应的信息。如果使用的是Spring Boot,则可以在application.properties中直接进行配置。
spring.data.rest.basePath=/api
这里只配置了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;}
比如在实体对象User中,我们不希望password 序列化未JSON,在上篇博客中说到,Spring Data REST默认使用的是JackSon,则我们就可以使用在需要隐藏的字段添加@JsonIgnore即可
2、@Projections
@Projection(name="list",types=User.class)public interface ListUser { String getName(); long getId();}
也可以通过@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 }}
@Projection还可以用来建立虚拟列
@Projection(name="virtual",types=User.class)public interface VirtualUser { @Value("#{target.name} #{target.age}") String getFullInfo();}
这里把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 }}
@Projection定义的数据格式还可以直接配置到Repository之上,就像下面代码中的这样
@RepositoryRestResource(path="user",excerptProjection=ListUser.class)public interface UserRepository extends JpaRepository<User, Long>{}
配置之后返回的JSON数据会按照ListUser定义的数据格式进行输出
三、屏蔽自动化方法
在实际生产环境中,不会轻易的删除用户数据,此时我们不希望DELETE的提交方式生效,可以添加@RestResource注解,并设置exported=false,即可屏蔽Spring Data REST的自动化方法
比如我们不想轻易的暴露按主键删除的方法,只需要写如下代码
@RepositoryRestResource(path="user",excerptProjection=ListUser.class)public interface UserRepository extends JpaRepository<User, Long>{ @RestResource(exported = false) @Override public void delete(Long id);}
- Spring Data REST入门(三):自定义配置
- Spring Data REST入门(四):自定义查询(上)
- Spring Data REST入门(二):环境搭建+实战演练
- spring-data-solr 入门配置
- 8.3 Spring Data REST
- Spring Data REST
- Spring Data REST入门(一):两行代码搞定RESTFul
- Spring-data-rest 和Spring-data-jpa
- spring boot data rest 3
- SpringBoot之Spring Data REST
- Spring Boot 系列(三)属性配置&自定义属性配置
- spring boot 使用 spring data rest
- Spring入门学习(三)logback配置按天打日志
- Spring Data REST + GemFire + AngularJS Integration
- Spring Data Rest如何暴露ID字段
- Spring Data Rest学习篇----@Excerpts
- Spring Data Rest学习篇----@Projection
- Spring Data Rest学习篇----@Repository
- Visual Studio的使用技巧
- 支付宝 error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
- poi处理excel遇到的坑
- CRC算法
- JS对省市按首字母排序
- Spring Data REST入门(三):自定义配置
- Hadoop+HBase+ZooKeeper三者关系与安装配置
- iOS UISearchBar简介 —— HERO博客
- css3渐变之条纹背景
- 为Linux命令设置快捷键
- Qt Qwdget 汽车仪表知识点拆解3 进度条编写
- Http协议与TCP协议简单理解
- 【Unity】浅谈Awake函数与Start函数之间的区别
- android 实现伸缩布局效果