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
|
@Configuration public 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
|
@Entity public 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 ...
随机推荐
- DataSnap Demo:TFDConnection、最大连接数、客户端回叫功能、多线程模拟、压力测试等
一.限制最大连接数,并验证来访者用户身份: procedure TServerContainer1.DSServer1Connect( DSConnectEventObject: TDSConnect ...
- GridView分页排序
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridviewPage.asp ...
- Django的templates模版
前面介绍的都是django.http.HttpResponse 把内容直接返回到网页上,这次介绍通过渲染模版的方法来显示内容 步骤: 1.创建一个项目(略) 2.创建一个app(略) 3.添加项目到s ...
- 【转】O'Reilly Java系列书籍建议阅读顺序(转自蔡学庸)
Learning Java the O'Reilly's Way (Part I) Java 技术可以说是越来越重要了,不但可以用在计算机上,甚至连电视等家电用品,行动电话.个人数字助理(PDA)等电 ...
- esp8266的STM32驱动
esp8266的STM32驱动,数据发送接收由DMA完成,释放CPU. 目前只能发送返回消息为成功或失败的AT命令,并判断是否成功,详见esp8266_cmd():其它返回消息不可预知的命令(如查看A ...
- dom树的介绍,及原理分析
三.解析和DOM树的构建 1.解析: 由于解析渲染引擎是一个非常重要的过程,我们将会一步步的深入,现在让我们来介绍解析. 解析一个文档,意味着把它转换为一个有意义的结构——代码可以了解和使用的东西,解 ...
- Maven 解决JAR包冲突
在JAR 冲突的情况下, 利用Eclipse方式解决JAR包冲突时比较方便简洁的,步骤如下 1. 在Eclipse 中打开pom.xml , 选择 “Dependency Hierarchy” 2 ...
- Samza文档翻译 : Comparison Introduction
http://samza.incubator.apache.org/learn/documentation/0.7.0/comparisons/introduction.html 这里有一些使得Sam ...
- 进阶:使用 EntityManager
JPA中要对数据库进行操作前,必须先取得EntityManager实例,这有点类似JDBC在对数据库操作之前,必须先取得Connection实例,EntityManager是JPA操作的基础,它不是设 ...
- Tarjan+模板
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #in ...