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
{
  "_links":{
  "self":{
  }
},
  "id":1,
  "name":"bar"
}

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 如何显示主键的更多相关文章

  1. Spring中获取数据库表主键序列

    在程序开发中,我们经常有写数据库表的操作,数据表中经常带有主键自增序列,如何获取自增序列.spring中提供了相应的类 DataFieldMaxValueIncrementer. DataFieldM ...

  2. Spring boot JPA 用自定义主键策略 生成自定义主键ID

    最近学习Spring boot JPA 学习过程解决的一些问题写成随笔,大家一起成长.这次遇到自定义主键的问题 package javax.persistence; public enum Gener ...

  3. Data Base sqlServer 组合主键

    sqlServer   组合主键 创建表时: create table Person ( Name1 ) not null ,Name2 ) not null primary key(Name1,Na ...

  4. spring boot——关于一个Mysql主键的问题

    问题是这样的: 我现在有一个被@Entity标记的类TimeLine,其中id为主键. TimeLineController中有一个接收post请求的add()方法,这个方法会接受客户端传来的一个表单 ...

  5. spring Jdbc自己主动获取主键。

    学习了下springjdbc,感觉挺有用的,相对来说springjdbc 扩展性相当好了 package com.power.dao; import java.lang.reflect.Paramet ...

  6. Spring Data - Spring Data JPA 提供的各种Repository接口

    Spring Data Jpa 最近博主越来越懒了,深知这样不行.还是决定努力奋斗,如此一来,就有了一下一波复习 演示代码都基于Spring Boot + Spring Data JPA 传送门: 博 ...

  7. MySQL Index--InnoDB引擎的主键索引

    查看表主键信息 ## 查看表主键信息 SELECT t.TABLE_NAME, t.CONSTRAINT_TYPE, c.COLUMN_NAME, c.ORDINAL_POSITION FROM IN ...

  8. SpringJPA主键生成采用自定义ID,自定义ID采用年月日时间格式

    自定义主键生成策略 在entity类上添加注解 @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "custom ...

  9. MySQL老是提示视图没有主键

    写了一个视图,每次打开都提示没有主键.我又不想更新视图,根本不关心这个,但每次都提示,很烦. 网上找到解决办法,就是关闭提示: Windows 和 Linux:选择工具 > 选项,并在外观 &g ...

随机推荐

  1. Spark Streaming揭秘 Day9 从Receiver的设计到Spark框架的扩展

    Spark Streaming揭秘 Day9 从Receiver的设计到Spark框架的扩展 Receiver是SparkStreaming的输入数据来源,从对Receiver整个生命周期的设计,我们 ...

  2. 【 socke】C# socket端口复用-多主机头绑定

    什么是端口复用: 因为在winsock的实现中,对于服务器的绑定是可以多重绑定的,在确定多重绑定使用谁的时候,根据一条原则是谁的指定最明确则将包递交给谁,而且没有权限之分.这种多重绑定便称之为端口复用 ...

  3. POJ 3701 概率DP

    给定2^n 支足球队进行比赛,n<=7. 队伍两两之间有一个获胜的概率,求每一个队伍赢得最后比赛的概率是多少? 状态其实都是很显然的,一开始觉得这个问题很难啊,不会.dp[i][j] 表示第i支 ...

  4. Unity3d之Http通讯GET方法和POST方法

    (一)GET方法 IEnumerator SendGet(string _url) { WWW getData = new WWW(_url); yield return getData; if(ge ...

  5. bnuoj 27987 Record of the Attack at the Orbit (模拟)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=27987 [题意]:给定坐标输出图形 [题解]:处理坐标上的小技巧 [code]: #include ...

  6. JavaScript与DOM的关系

    JavaScript与浏览器的工作 1.浏览器获取并加载你的页面,从上至下解析它的内容. 遇到JavaScript时,浏览器会解析代码,检查它的正确性,然后执行代码. 浏览器还会建立一个HTML页面的 ...

  7. centos最小安装 setuptools安装

    centos运行不了setup?那安装setuptool吧,可以节省很多系统管理的时间. #安装setuptoolyum install setuptool#可以发现执行setup后不全,再安装一个用 ...

  8. Akka官方文档翻译:Cluster Specification

    参加了CSDN的一个翻译项目,翻译Akka的文档.CSDN提供的翻译系统不好使,故先排版一下放在博客上. 5.1 集群规范 注意:本文档介绍了集群的设计理念.它分成两部分,第一部分描述了当前已经实现的 ...

  9. tomcat配置301重定向(urlRewrite URL重写)

    tomcat默认情况下不带www的域名是不会跳转到带www的域名的,而且也无法像apache那样通过配置.htaccess来实现.如果想要把不带“www'的域名重定向到带”www"域名下,又 ...

  10. Hardwood Species

    http://poj.org/problem?id=2418 #include<cstdio> #include<cstring> #include<string> ...