zoukankan      html  css  js  c++  java
  • 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
    {
      "_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);
                }
            };
        }
    
    }
    

      

    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对压力,能够挑灯夜战不眠不休;面对困难,能够迎难而上挑战自我。他 们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想“用智慧开创属于自己的事业”。我想说的是,其 实我是一个程序员
  • 相关阅读:
    WPF:ListView 分组合并
    WPF:ListView 分页
    SQL
    多线程27 -- ReentrantLock
    String 对象常用方法及属性 详细介绍
    js中Array 对象方法 详细介绍
    js--运算符与或非 及 if判断条件、隐式转换 介绍
    js-函数表达式和函数声明的区别
    js预编译案例分析
    js--万物皆对象
  • 原文地址:https://www.cnblogs.com/kms1989/p/5809187.html
Copyright © 2011-2022 走看看