Spring data rest 如何显示主键
How to expose the resourceId with Spring-Data-Rest?
Spring-Data-Rest is a quite new project in the Spring family of pivotal. The intention of this project is to reduce the boilercode of controllers and services when you need only CRUD methods on an entity for your REST resources. – Quote from project page is “Spring-Data-Rest makes it easy to expose JPA based repositories as RESTful endpoints.”
One requirement I had was to expose the CRUD identifier and database primary key which is annotated with @Id. In my case that was needed because the field was functional required. For that I had to expose it because at the standard configuration the ID field is only visible on the resource path, but not on the JSON body.
To expose it you need to configure your RepositoryRestMvcConfiguration like that:
|
1
2
3
4
5
6
7
8
|
@Configurationpublic class MyCoolConfiguration extends RepositoryRestMvcConfiguration { @Override protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) { config.exposeIdsFor(FooEntity.class); }} |
The entity class could look like that:
|
1
2
3
4
5
6
7
8
9
|
@Entitypublic class FooEntity { @Id @GeneratedValue(strategy= GenerationType.AUTO) Long id; String name;} |
With this configuration you will receive your entity id back.
|
1
2
3
4
5
6
7
8
9
|
Additional Comment from a Spring-Developer: URI stands for unique, resource, *identifier* – The URI *is* the id. What I expose here is a database internals.
转自:How to expose the resourceId with Spring-Data-Rest?
另一种:
package net.huuat.micro.base.schedule; import net.huuat.micro.base.schedule.domain.TJobConf; 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; @Configuration
public class SpringDataRestConfig {
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() { return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
config.exposeIdsFor(TJobConf.class);
}
};
} }
Spring data rest 如何显示主键的更多相关文章
- Spring中获取数据库表主键序列
在程序开发中,我们经常有写数据库表的操作,数据表中经常带有主键自增序列,如何获取自增序列.spring中提供了相应的类 DataFieldMaxValueIncrementer. DataFieldM ...
- Spring boot JPA 用自定义主键策略 生成自定义主键ID
最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...
- Data Base sqlServer 组合主键
sqlServer 组合主键 创建表时: create table Person ( Name1 ) not null ,Name2 ) not null primary key(Name1,Na ...
- spring boot——关于一个Mysql主键的问题
问题是这样的: 我现在有一个被@Entity标记的类TimeLine,其中id为主键. TimeLineController中有一个接收post请求的add()方法,这个方法会接受客户端传来的一个表单 ...
- spring Jdbc自己主动获取主键。
学习了下springjdbc,感觉挺有用的,相对来说springjdbc 扩展性相当好了 package com.power.dao; import java.lang.reflect.Paramet ...
- Spring Data - Spring Data JPA 提供的各种Repository接口
Spring Data Jpa 最近博主越来越懒了,深知这样不行.还是决定努力奋斗,如此一来,就有了一下一波复习 演示代码都基于Spring Boot + Spring Data JPA 传送门: 博 ...
- MySQL Index--InnoDB引擎的主键索引
查看表主键信息 ## 查看表主键信息 SELECT t.TABLE_NAME, t.CONSTRAINT_TYPE, c.COLUMN_NAME, c.ORDINAL_POSITION FROM IN ...
- SpringJPA主键生成采用自定义ID,自定义ID采用年月日时间格式
自定义主键生成策略 在entity类上添加注解 @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "custom ...
- MySQL老是提示视图没有主键
写了一个视图,每次打开都提示没有主键.我又不想更新视图,根本不关心这个,但每次都提示,很烦. 网上找到解决办法,就是关闭提示: Windows 和 Linux:选择工具 > 选项,并在外观 &g ...
随机推荐
- (转载)MS SQL Server 未公开的加密函数有哪些?
MS SQL Server 未公开的加密函数有哪些? 以下的文章是对MS SQL Server 未公开的加密函数的具体操作,如果你对其相关的实际操作有兴趣的话,你就可以点击了. MS SQL Serv ...
- vector 内部方法大全 学习(初学者的参考资料)
1 vector构造函数:也就是如何对一个vector对象进行初始化 ////////////////////////////代码//////////////////////////////// ...
- call callb callp区别
对于刚刚接触ILE模式开发的初级菜鸟而言,想要搞清楚这三者的区别还是有点难度的.网上虽然一些帖子对这三者进行了比较,但是这些帖子或是语焉不详,或是高度概括.对于老鸟来说或许已经足矣,但是对于初级菜鸟而 ...
- flash memory
数据删除不是以单个的字节为单位而是以固定的区块为单位(注意:NOR Flash 为字节存储.),区块大小一般为256KB到20MB. 由于其断电时仍能保存数据,闪存通常被用来保存设置信息,如在电脑的B ...
- Java实现MySQL在线管理
问题: 1.如何实现在线创建MySQL数据库.表: 2.如何实现在线执行sql语句? 思路: 1.可以通过java.sql.Statement类的executeUpdate(String sql)方法 ...
- pl/sql插入报错
用pl/sql 命令的方法导入文件,发现一只提示文件报错.报Error reading file错误. 原来: 在pl/sql工具->导入表里的sql插入方式下,可以选择“使用命令窗口”和“使用 ...
- 移动APP学习笔记一
登录 ●登录用户名和密码错误时,界面有提示信息 ●用户主动退出登录后,下次启动APP时,应该进入登录界面 ●对于支持自动登录的APP,数据交换时 ,是否能自动登录成功且数据库操作无误 ●密码更改后, ...
- EF4.1之覆盖EF的默认的约定
覆盖EF默认的约定可以通过两种方式: 1.拦截模型构建器,使用流畅的API 2.通过给 类添加标签 好的,我还用之前定义的订单类来做例子: public class Order { public in ...
- 【更新链接】U盘启动制作工具(UDTOOL) v3.0.2014.0427
[校验值] 文件: UDTOOLV3_Setup.exe大小: 525 MB版本: 3.0.2014.0427时间: 2014年4月27日MD5: 2E5187B7D9081E8A69B4DC45C8 ...
- 对jQuery.isArray方法的分析
jQuery.isArray方法应于判断是不是数组,是的话返回true,否则返回false.调用如:jQuery.isArray([]),返回true.其实现源码如下: isArray: Array. ...