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. (转)MVC 3 数据验证 Model Validation 详解

    继续我们前面所说的知识点进行下一个知识点的分析,这一次我们来说明一下数据验证.其实这是个很容易理解并掌握的地方,但是这会浪费大家狠多的时间,所以我来总结整理一下,节约一下大家宝贵的时间. 在MVC 3 ...

  2. sqlplus中"-S"和"-L"用法

    Usage: SQLPLUS [option] [logon] [start] <option> ::= -H | -V | [ [-L] [-M ] [-R ] [-S] ]   &qu ...

  3. vi使用教程

    Vi有3种模式: 命令模式——命令操作 插入模式——进入vi之后,输入i/a/o,按Esc键,进入命令模式 编辑模式——:set nu, 以回车结束 1.插入 a - 光标后插入 A - 本行末尾插入 ...

  4. VS2010中无法嵌入互操作类型“......”,请改用适用的接口的解决方法

  5. ios Base64编解码工具类及使用

    为了避免明码传递http内容,可以用base64编码后传输,收到方再解码,也方便了2进制数据的字符串式传输. 对于ios来说,google给提供了一个很好的工具类,方便进行base64编解码,当然也可 ...

  6. [译] ASP.NET 生命周期 – ASP.NET 请求生命周期(二)

    ASP.NET 请求生命周期 全局应用类也可以用来跟踪每个独立请求的生命周期,包括请求从 ASP.NET 平台传递到 MVC 框架.ASP.NET 框架会创建一个定义在 Global.asax 文件中 ...

  7. DNF技能贴图的研究

    一直在猜想DNF的技能贴图怎么贴的,靠在游戏里慢慢移动确定技能的偏移太费时间了.前段发现了“可视坐标生成”这软件,针对DNF改衣服,装备款式的小工具,就自己写了个类似的. 从图上看,技能的域中心点和人 ...

  8. [转]LINQ语句之Select/Distinct和Count/Sum/Min/Max/Avg

    在讲述了LINQ,顺便说了一下Where操作,这篇开始我们继续说LINQ语句,目的让大家从语句的角度了解LINQ,LINQ包括LINQ to Objects.LINQ to DataSets.LINQ ...

  9. The 11th Zhejiang Provincial Collegiate Programming Contest->Problem A:A - Pokemon Master

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3776 题意:比较两组数据的总和大小. #include <iostr ...

  10. PLSQL显示乱码-无法进行中文条件查询解决

    PLSQL显示乱码-无法进行中文条件查询解决 原因: PLSQL乱码问题皆是ORACLE服务端字符集编码与PLSQL端字符集编码不一致引起.类似乱码问题都可以从编码是否一致上面去考虑. 解决: 1. ...