• Spring Data Rest如何暴露ID字段


    package com.example.demo.config;
    
    import com.example.demo.model.Comp;
    import com.example.demo.model.Person;
    import org.apache.catalina.User;
    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;
    
    /**
     * @Author:LJ
     * @Description:为了解决Spring Data Rest不暴露ID字段的问题。
     * 参考:http://tommyziegler.com/how-to-expose-the-resourceid-with-spring-data-rest/
     * @Date: 2018/3/21
     * @Modified By:
     */
    @Configuration
    public class SpringDataRestConfig {
        @Bean
        public RepositoryRestConfigurer repositoryRestConfigurer() {
            return new RepositoryRestConfigurerAdapter() {
                @Override
                public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
                    config.exposeIdsFor(User.class, Person.class, Comp.class);
                }
            };
        }
    }

    场景问题如下:

    <!--通过REST 风格接口 暴露Spring Data数据库表-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-rest</artifactId>
            </dependency>
    package com.example.demo.model;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    public class User {
        @Id
        @GeneratedValue
        private long id;
        @Column(nullable = false, unique = true)
        private String userName;
        @Column(nullable = false)
        private String password;
        @Column(nullable = false)
        private int age;
    
        public long getId() {
            return id;
        }
    
        public User setId(long id) {
            this.id = id;
            return this;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public User setUserName(String userName) {
            this.userName = userName;
            return this;
        }
    
        public String getPassword() {
            return password;
        }
    
        public User setPassword(String password) {
            this.password = password;
            return this;
        }
    
        public int getAge() {
            return age;
        }
    
        public User setAge(int age) {
            this.age = age;
            return this;
        }
    }
    package com.example.demo.repository;
    
    import com.example.demo.model.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface UserRepository extends JpaRepository<User, Long> {
    
        User findById(long id);
    
        void deleteById(long id);
    }

    运行http://localhost:8080/users,结果

    {
      "_embedded" : {
        "user" : [ {
          "age" : "12",
         "password":"12345""user_name":"lisi"
        }, {
          "age" : "25",
          ......
    }
  • 相关阅读:
    USACO 3.3.1 Riding the Fences 骑马修栅栏(欧拉回路)
    USACO 3.2.6 Sweet Butter 香甜的黄油(最短路)
    USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)
    USACO 1.3.3 Calf Flac(Manacher算法)
    USACO 1.2.2 Transformations 方块转换
    小希的迷宫(并查集判环)
    Is It A Tree?(并查集)
    Manacher算法——求最长回文子串
    Live Love(思维)
    Longge's problem(欧拉函数应用)
  • 原文地址:https://www.cnblogs.com/liaojie970/p/8619253.html
Copyright © 2020-2023  润新知