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 ...
随机推荐
- golang debug调试
1. debug by gdb: office doc download the runtime-gdb file. $ wget -q -O - https://golang.org/src/run ...
- 20145337 《网络对抗技术》Web基础
20145337 <网络对抗技术>Web基础 一.实验后回答问题 什么是表单? 表单是HTML的一个重要部分,主要用于将用户输入的信息提交到服务器.如果是普通的HTML页面,则当浏览器提出 ...
- win7 + MySQL 5.6.35 免安装版部署
之前项目开发一直用的asp.net技术,所以数据库自然而然的就用的Sql Server了,最近想着手看一下MySQL数据库,部署免安装版的MySQL的过程记录一下. 准备工作:window 7 6 ...
- 理解 python 中__name__ = '__main__' 的作用
很多新手刚开始学习python的时候经常会看到python 中__name__ = \'__main__\' 这样的代码,可能很多新手一开始学习的时候都比较疑惑,python 中__name__ = ...
- 【资源】分享一个最新版sublime 3143的注册码,亲测可用
注:请勿用作商业用途,有能力者请购买正版!!! —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD9 ...
- Linux系统编程--文件描述符的复制dup()和dup2()【转】
本文转载自:http://blog.csdn.net/tennysonsky/article/details/45870459 dup() 和 dup2() 是两个非常有用的系统调用,都是用来复制一个 ...
- PyQt5 - 01 使用qt creator创建第一个pyqt5界面程序
1. 安装Qt Creator qt creator下载点我 2. 利用Qt Creator创建界面 点击文件 -> 新建文件或项目 选择Qt -> Qt设计师界面类 选择一个模版,创建一 ...
- windows下如何解决chrome浏览器左下角总提示'Downloading proxy script'的问题
答:关掉LAN settings中的Automatically detect settings选项和Use automatic configuration script选项,如下图
- Windows 上安装 pip
1 从 https://pypi.python.org/pypi/pip#downloads 下载安装包 pip-9.0.1.tar.gz 2 解压 pip-9.0.1.tar.gz 3 用CMD ...
- 入门dp总结
写这篇博文主要是为了归纳总结一下dp的有关问题(不定期更新,暑假应该会更的快一些) 会大概讲一下思路,不会事无巨细地讲 另一篇是平时做过的一些dp题,这篇博客里面提到的题都有题解放在那边:https: ...