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 ...
随机推荐
- Android图像处理2
此次实验主要通过Android中的方法获取输入的颜色矩阵的值,更改后赋值给图片中的颜色矩阵更改图片效果.具体的布局的方法跟笔记1种差不多,只不过这里要添加一个供用户输入的GridView <Gr ...
- javac mac 终端乱码
java和javac在简体中文的Mac OSX的终端(Terminal.app)环境下,默认是以GBK编码的中文输出各种诸如语法错误,数组访问越界之类的信息.但是,Mac的终端的默认编码是UTF-8, ...
- 电网SVG简介
目 录1. 范围 12. 规范性引用文件 13. 缩略语 14. 本标准涉及的图形交换特征 ...
- mysql ab 压力测试的解读(转)
本文转自:http://www.cnblogs.com/lemtree/articles/1676641.html. 就是APACHE自带的测试工具AB(apache benchmark).在APAC ...
- 两年的坚持,最后还是决定将ISoft开源
还记得2011年9月份,我在上大四,本来想着考研能上个好点的学校,可我怎么就不愿去自习室上自习.每天晚上睡觉前都告诉自己明天早晨一定早起去上自习,但又每次醒来都不想起床啊,懒,没办法.睡到不想再睡了才 ...
- 【BZOJ 1070】[SCOI2007]修车
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使 ...
- 这是我用Microsoft Word 2010 直接发布的测试用博客
目的:如题所示. 那么先试试拷贝一段网页内容,发布后观察各种格式的显示效果如何. 下面的文字来自中国网新闻,地址是http://news.china.com.cn/2015-10/23/conte ...
- 1191: [HNOI2006]超级英雄Hero - BZOJ
Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回 ...
- eclipse/MyEclipse 日期格式、注释日期格式、时区问题
eclipse/MyEclipse 日期格式.注释日期格式.时区问题 在eclipse/MyEclipse中,如果你的注释或是运行System.out.print(new java.util.Date ...
- SNMP的工作原理&软件开发
SNMP(Simple Network Management Protocol,简单网络管理协议)首先是由IETF的研究小组为了解决Internet上的路由器管理问题而提出的.SNMP的设计原则是简单 ...