Spring Data Rest如何暴露ID字段
package com.example.demo.config; import com.example.demo.model.Comp;
import com.example.demo.model.Person;
import org.apache.catalina.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; /**
* @Author:LJ
* @Description:为了解决Spring Data Rest不暴露ID字段的问题。
* 参考:http://tommyziegler.com/how-to-expose-the-resourceid-with-spring-data-rest/
* @Date: 2018/3/21
* @Modified By:
*/
@Configuration
public class SpringDataRestConfig {
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(User.class, Person.class, Comp.class);
}
};
}
}
场景问题如下:
<!--通过REST 风格接口 暴露Spring Data数据库表-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
package com.example.demo.model; import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class User {
@Id
@GeneratedValue
private long id;
@Column(nullable = false, unique = true)
private String userName;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private int age; public long getId() {
return id;
} public User setId(long id) {
this.id = id;
return this;
} public String getUserName() {
return userName;
} public User setUserName(String userName) {
this.userName = userName;
return this;
} public String getPassword() {
return password;
} public User setPassword(String password) {
this.password = password;
return this;
} public int getAge() {
return age;
} public User setAge(int age) {
this.age = age;
return this;
}
}
package com.example.demo.repository; import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { User findById(long id); void deleteById(long id);
}
运行http://localhost:8080/users,结果
{
"_embedded" : {
"user" : [ {
"age" : "12",
"password":"12345",
"user_name":"lisi"
}, {
"age" : "25",
......
}
Spring Data Rest如何暴露ID字段的更多相关文章
- Spring data mongodb ObjectId ,根据id日期条件查询,省略@CreatedDate注解
先看看ObjectId 的json 结构,非常丰富,这里有唯一机器码,日期,时间戳等等,所以强烈建议ID 使用 ObjectId 类型,并且自带索引 Spring data mongodb 注解 @C ...
- spring data jpa 原生sql 别名字段无法注入
开发四年只会写业务代码,分布式高并发都不会还做程序员?->>> 在使用entityManager.createNativeQuery(sql,User.class)这个方法时, ...
- Elasticsearch基本用法(2)--Spring Data Elasticsearch
Spring Data Elasticsearch是Spring Data项目下的一个子模块. 查看 Spring Data的官网:http://projects.spring.io/spring-d ...
- Spring Data学习(一):初识
目录 前言 添加Spring Data 配置pom.xml 配置数据库相关信息(application.properties) 配置数据库信息 配置自动根据实体类在数据库创建表 创建User.java ...
- 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分
Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...
- Spring Data Elasticsearch基本使用
目录 1. 创建工程 2. 配置application.yaml文件 3. 实体类及注解 4. 测试创建索引 5. 增删改操作 5.1增加 5.2 修改(id存在就是修改,否则就是插入) 5.3 批量 ...
- 3.4_springboot2.x整合spring Data Elasticsearch
Spring Data Elasticsearch 是spring data对elasticsearch进行的封装. 这里有两种方式操作elasticsearch: 1.使用Elasticsearch ...
- SprignBoot整合Spring Data Elasticsearch
一.原生java整合elasticsearch的API地址 https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.2/java ...
- spring data mongodb中,如果对象中的属性不想加入到数据库字段中
spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://ww ...
随机推荐
- 护眼色的RGB值和颜色代码汇总
源: 网上流行护眼色的RGB值和颜色代码汇总 网上流行护眼色的RGB值和颜色代码 在搜索引擎搜“护眼色”,就会搜出一堆关于保护眼睛的屏幕颜色文章,说的统统是一种颜色,有点像绿豆沙的颜色.方法就是在屏幕 ...
- 20145335郝昊《网络攻防》Exp4 Adobe阅读器漏洞攻击
20145335郝昊<网络攻防>Exp4 Adobe阅读器漏洞攻击 实验内容 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 本次攻击对象为:windows xp sp ...
- 2018-2019-1 20189218《Linux内核原理与分析》第七周作业
task_struck数据结构 在Linux内核中,通过task_struct这个结构体对进程进行管理,我们可以叫他PCB或者进程描述符.这个结构体定义在include/linux/sched.h中. ...
- Email移动的原理
1.从数据库中得到被移动邮件的uid: 2.选择移动邮件所属folder,即SelectFolder; 3.调用copymessage(path,vmime::net::messageset::byU ...
- JavaScript 开闭原则OCP
代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- fread
快速读入? 不知道 反正只是贴过来的 以后或许会用到? #define FI(n) FastIO::read(n) namespace FastIO { << ; ]; int bi = ...
- jquery 浏览器打印
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 如何设置datatable的宽度
本文为博主原创,未经允许不得转载: datatable插件具有很好的自动适应性,在大多数情况下可以自适应,完美的展示列表内容. 实现其自适应的其特性为:"bAutoWidth": ...
- 【TCP/IP详解 卷一:协议】第十七章 TCP:传输控制协议
本章作为TCP的入门章节,简单的概述了一些TCP的知识,和TCP数据报的首部格式. TCP 最重要的特性:reliable. 17.1 引言 本章介绍的是 TCP为应用层提供的服务. 17.2 TCP ...
- shell while 语句
普通循环格式: while condition do command done 例子一 #!/bin/bash )) do echo $int let "int++" done 结 ...