spring data rest 使用示例


spring data rest 使用示例

 

官网:https://docs.spring.io/spring-data/rest/docs/3.3.1.RELEASE/reference/html/#reference

应用:自动将repository转换为rest资源,可不提供controller层接口直接访问数据,支持spring data jpa、mongoDB、Neo4j等

 

 

***********************

引入 jar 包

 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

 

RepositoryRestProperties:自动配置属性

@ConfigurationProperties(prefix = "spring.data.rest")
public class RepositoryRestProperties {

	private String basePath;           //根路径

	private Integer defaultPageSize;   //默认页记录数
	private Integer maxPageSize;       //页最大记录数

	private String pageParamName;      //页参数名称
	private String limitParamName;     //页记录数名称
	private String sortParamName;      //排序参数

	private RepositoryDetectionStrategies detectionStrategy = RepositoryDetectionStrategies.DEFAULT;
	private MediaType defaultMediaType;
	private Boolean returnBodyOnCreate;
	private Boolean returnBodyOnUpdate;
	private Boolean enableEnumTranslation;

 

 

***********************

示例

 

***************

pojo 层

 

Person

@Data
@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private Integer age;
}

 

***************

dao 层

 

PersonRepository

public interface PersonRepository extends JpaRepository<Person,Integer> {
}

 

 

***********************

使用测试

 

获取所有数据:localhost:8080/persons

{
  "_embedded": {
    "persons": [
      {
        "name": "瓜田李下",
        "age": 20,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/1"
          },
          "person": {
            "href": "http://localhost:8080/persons/1"
          }
        }
      },
      {
        "name": "瓜田李下2",
        "age": 21,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/2"
          },
          "person": {
            "href": "http://localhost:8080/persons/2"
          }
        }
      },
      {
        "name": "瓜田李下3",
        "age": 23,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/3"
          },
          "person": {
            "href": "http://localhost:8080/persons/3"
          }
        }
      },
      {
        "name": "瓜田李下4",
        "age": 24,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/4"
          },
          "person": {
            "href": "http://localhost:8080/persons/4"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/persons"
    },
    "profile": {
      "href": "http://localhost:8080/profile/persons"
    }
  },
  "page": {
    "size": 20,
    "totalElements": 4,
    "totalPages": 1,
    "number": 0
  }
}

 

 

获取指定记录数据:localhost:8080/persons/1

{
  "name": "瓜田李下",
  "age": 20,
  "_links": {
    "self": {
      "href": "http://localhost:8080/persons/1"
    },
    "person": {
      "href": "http://localhost:8080/persons/1"
    }
  }
}

 

 

提交数据:localhost:8080/persons

header:Content-Type application/json

body:

{
    "id": 6,
    "name": "瓜田李下6",
    "age": "26"
}

           

 

 

更新数据:localhost:8080/persons/6,put提交

header:Content-Type application/json

body:

{
    "id": 6,
    "name": "瓜田李下6",
    "age": "20"
}

          

 

 

删除数据:localhost:8080/persons/7

 

分页查询:localhost:8080/persons?page=0&size=2

{
  "_embedded": {
    "persons": [
      {
        "name": "瓜田李下",
        "age": 20,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/1"
          },
          "person": {
            "href": "http://localhost:8080/persons/1"
          }
        }
      },
      {
        "name": "瓜田李下2",
        "age": 21,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/2"
          },
          "person": {
            "href": "http://localhost:8080/persons/2"
          }
        }
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/persons?page=0&size=2"
    },
    "self": {
      "href": "http://localhost:8080/persons?page=0&size=2"
    },
    "next": {
      "href": "http://localhost:8080/persons?page=1&size=2"
    },
    "last": {
      "href": "http://localhost:8080/persons?page=2&size=2"
    },
    "profile": {
      "href": "http://localhost:8080/profile/persons"
    }
  },
  "page": {
    "size": 2,
    "totalElements": 6,
    "totalPages": 3,
    "number": 0
  }
}

 

 

排序:localhost:8080/persons?page=0&size=2&sort=age,desc

{
  "_embedded": {
    "persons": [
      {
        "name": "瓜田李下6",
        "age": 26,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/6"
          },
          "person": {
            "href": "http://localhost:8080/persons/6"
          }
        }
      },
      {
        "name": "瓜田李下5",
        "age": 25,
        "_links": {
          "self": {
            "href": "http://localhost:8080/persons/5"
          },
          "person": {
            "href": "http://localhost:8080/persons/5"
          }
        }
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8080/persons?page=0&size=2&sort=age,desc"
    },
    "self": {
      "href": "http://localhost:8080/persons?page=0&size=2&sort=age,desc"
    },
    "next": {
      "href": "http://localhost:8080/persons?page=1&size=2&sort=age,desc"
    },
    "last": {
      "href": "http://localhost:8080/persons?page=2&size=2&sort=age,desc"
    },
    "profile": {
      "href": "http://localhost:8080/profile/persons"
    }
  },
  "page": {
    "size": 2,
    "totalElements": 6,
    "totalPages": 3,
    "number": 0
  }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值