本文共 9948 字,大约阅读时间需要 33 分钟。
4.0.0 org.springframework.boot spring-boot-starter-parent 1.3.0.M2 com.pyc springbootrest 0.0.1-SNAPSHOT springbootrest jar Demo project for Spring Boot 1.8 UTF-8 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-data-rest com.oracle ojdbc6 11.2.0.2.0 org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin
spring.datasource.driverClassName=oracle.jdbc.OracleDriverspring.datasource.url=jdbc\:oracle\:thin\:@localhost\:1521\:xespring.datasource.username=bootspring.datasource.password=bootspring.jpa.hibernate.ddl-auto=updatespring.jpa.show-sql=truespring.jackson.serialization.indent_output=truedebug=truelogging.file=log.loglogging.level.org.springframework.web=DEBUG
package com.pyc.springbootrest.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Person { @Id @GeneratedValue private Long id; private String name; private Integer age; private String address; public Person() { super(); } public Person(Long id, String name, Integer age, String address) { super(); this.id = id; this.name = name; this.age = age; this.address = address; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; }}
package com.pyc.springbootrest.dao;import com.pyc.springbootrest.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.query.Param;;import org.springframework.data.rest.core.annotation.RestResource;public interface PersonRepository extends JpaRepository{ @RestResource(path = "nameStartsWith", rel = "nameStartsWith") Person findByNameStartsWith(@Param("name") String name);}
打开谷歌浏览器,点击地址栏右边的 Postman REST client 图标打开插件,初始如下图:
在 Enter request URL here 输入框输入 localhost:8080/persons, 方法选择 GET,然后点击 SEND 按钮拉取数据,这个 URL 获取的数据就是 PERSON 表的所有数据,如下:
如果想获取单一对象,也就是数据表中的某一条记录,则可以用记录的 id 来获取,具体操作就是在上面的那个 URL 加上 /id 构成:localhost:8080/persons/id,例如获取 id 为 3 的,
使用刚刚的自定义接口方法查询,使用 URL:localhost:8080/persons/search/nameStartsWith?name=y,返回 JSON 格式数据内容如下:
{ "name": "yy", "age": 20, "address": "上海", "_links": { "self": { "href": "http://localhost:8080/persons/3" } }}
测试分页查询,使用 url=localhost:8080/persons/?page=2&size=4,page=2 表示获取分页后的第二页,size=4 表示每页有四条记录,获得的数据如下:
{ "_links": { "first": { "href": "http://localhost:8080/persons?page=0&size=4" }, "prev": { "href": "http://localhost:8080/persons?page=1&size=4" }, "self": { "href": "http://localhost:8080/persons" }, "last": { "href": "http://localhost:8080/persons?page=2&size=4" }, "search": { "href": "http://localhost:8080/persons/search" }},"_embedded": { "persons": [ { "name": "csdn", "age": 20, "address": "杭州", "_links": { "self": { "href": "http://localhost:8080/persons/22" } } }, { "name": "lqy", "age": 21, "address": "dh", "_links": { "self": { "href": "http://localhost:8080/persons/41" } } }, { "name": "林秋延", "age": 21, "address": "东海", "_links": { "self": { "href": "http://localhost:8080/persons/42" } } }, { "name": "test", "age": 32, "address": "ds", "_links": { "self": { "href": "http://localhost:8080/persons/8" } } } ]},"page": { "size": 4, "totalElements": 12, "totalPages": 3, "number": 2 }}
测试排序,使用 url=localhost:8080/persons/?sort=age,desc,以字段 age 为参考降序,如下图:
以上都是查询功能,接下来使用 POST 方法向数据库写入数据,使用 POST 方法为了让后台能够正确解析数据,需要先设置以下数据报头 Content-type 为 application/json ,URL 使用 localhost:8080/person
写入成功则会返回如下图:
status 为 201 Created,离开浏览器,打开 Navicat 连接 Oracle 数据库查看目标数据表的情况如下:
成功将数据写入数据库。
为了 REST 资源及时更新,使用 PUT 方法访问 localhost:8080/persons/43,当返回的 status 为 200 OK 就表示更新成功。
再来测试从数据库从删除记录,删除记录的 url=localhost:8080/persons/id,例如删除 id 为 41 和 21 的记录
当返回的 status 如上图红色方框中那样,便是没有出错,此时回到 Navicat 刷新查看数据表,则发现 id 为 41 和 21 的记录已经删除:
package org.springframework.boot.autoconfigure.data.rest;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.context.properties.ConfigurationProperties;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.RepositoryRestMvcConfiguration;import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;@Configurationpublic class SpringBootRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration { @Autowired( required = false ) private Jackson2ObjectMapperBuilder objectMapperBuilder; public SpringBootRepositoryRestMvcConfiguration() { } @Bean @ConfigurationProperties( prefix = "spring.data.rest" ) public RepositoryRestConfiguration config() { return super.config(); } protected void configureJacksonObjectMapper(ObjectMapper objectMapper) { if (this.objectMapperBuilder != null) { this.objectMapperBuilder.configure(objectMapper); } }}
spring.data.rest.base-path=/api
package com.pyc.springbootrest.dao;import com.pyc.springbootrest.domain.Person;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.query.Param;;import org.springframework.data.rest.core.annotation.RepositoryRestResource;import org.springframework.data.rest.core.annotation.RestResource;@RepositoryRestResource(path = "people")public interface PersonRepository extends JpaRepository{ @RestResource(path = "nameStartsWith", rel = "nameStartsWith") Person findByNameStartsWith(@Param("name") String name);}
转载地址:http://zlqgn.baihongyu.com/