Postman-Restfule
一,概念简述
Postman是一个软件
Restful:是一种软件架构风格,为了提供一组设计原则和约束条件,用于客户端和服务器交互的软件,
二,对于REST的理解
1,互联网软件的架构
2,REST———–>(Resource)Representational State Transfer “资源” 表现层状态转化
3,资源(Resource):可以使一段文本,图片,一首歌。。。。就是一个具体的实在。要获得他的资源就必须访问他的URL,因此URL就成了每一个资源的地址会独一无二的标识符
4,表现层(Representation):在这里表现层并不是指的是:例如文本的表现形式txt, 也可以用html 格式等。URL代表资源的实体,或者是代表资源的位置。他的具体表现形式,应该在HTTP请求的头信息中用Accept和Content-Type字段指定,这两个字段才是对表现层的表述
5,状态转化(State Transfer):
如果客户端想要操作服务器,必须通过某种手段,让服务器端发生“状态转化(数据和状态转化)”,而这种转化是建立在表现层之上的,所以就是表现层状态转化
6,HTTP协议里面:有四个基本操作方式
GET:用来获取资源
POST: 用来新建资源(也可以用来更新资源)
PUT:用来更新资源
DELETE:用来删除资源
7,总结;
Restful架构是什么:
1,每一个URL代表一种资源
2,客户端和服务器之间,传递这种资源的某种表现层
3,客户端通过这四个HTTP动词,对服务器资源进行操作,实现“表现层状态转化”
三,用springboot+jpa 搭建Restful框架
一,项目的总体分层
1,domain
package com.yztc.demo.domain;
import javax.persistence.*;
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column
private String name;
@Column
private String address;
@Column
private String photo;
public Employee() {
}
public Employee(String name, String address, String photo) {
this.name = name;
this.address = address;
this.photo = photo;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
", photo='" + photo + '\'' +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
}
2,repository
package com.yztc.demo.repository;
import com.yztc.demo.domain.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmployeeRepository extends JpaRepository<Employee,Integer>{
}
3,service
EmpService
package com.yztc.demo.service;
import com.yztc.demo.domain.Employee;
import com.yztc.demo.status.ResultMsg;
import java.util.List;
public interface EmpService {
/**
* 添加
* @param employee 参数是employee
* @return 返回一个对象,对象里面有状态码和属性两个属性
*/
public ResultMsg saveEmployee(Employee employee);
/**
* 查找所有的
* 不需要参数
* @return 返回一个list集合,集合里面是Employee实体类
*/
public List<Employee> findAllEmployee();
/**
* 根据id进行删除
* @param id 参数是id
* @return 返回一个对象。对象里面存放状态码,和信息
*/
public ResultMsg deleteEmployee(Integer id);
}
EmpServiceImpl
package com.yztc.demo.service;
import com.yztc.demo.domain.Employee;
import com.yztc.demo.repository.EmployeeRepository;
import com.yztc.demo.status.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeServiceImpl implements EmpService {
/**
* 注入EmployeeRepository,里面提供了增删改查的方法
*/
@Autowired
private EmployeeRepository employeeRepository;
/**
* 添加,添加成功返回数字1,添加失败返回数字-1
* @param employee 参数是employee
* @return
*/
@Override
public ResultMsg saveEmployee(Employee employee) {
try{
employeeRepository.save(employee);
return new ResultMsg(1,"添加成功");
}catch(Exception e){
e.printStackTrace();
}
return new ResultMsg(-1,"添加失败");
}
/**
* 查找所有
* @return 返回List集合
*/
@Override
public List<Employee> findAllEmployee() {
return employeeRepository.findAll();
}
/**
* 删除,根据id进行删除,所以参数是id,删除成功返回数字1,删除失败返回数字-1
* @param id 参数是id
* @return
*/
@Override
public ResultMsg deleteEmployee(Integer id) {
try{
employeeRepository.delete(id);
return new ResultMsg(1,"删除成功");
}catch(Exception e){
e.printStackTrace();
}
return new ResultMsg(-1,"删除失败");
}
}
4,status
package com.yztc.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Resufuldemo1Application {
public static void main(String[] args) {
SpringApplication.run(Resufuldemo1Application.class, args);
}
}
5,controller
package com.yztc.demo.controller;
import com.yztc.demo.domain.Employee;
import com.yztc.demo.service.EmpService;
import com.yztc.demo.status.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmployeeController {
/**
* 注入EmpService 以此来进行调用EmpService的方法
*/
@Autowired
private EmpService empService;
/**
* 添加
* @param employee 参数是employee实体类
* @return 返回对象
*/
@RequestMapping(value = "/addemployee",method = RequestMethod.POST)
public ResultMsg addEmployee(Employee employee){
System.out.println(employee);
return empService.saveEmployee(employee);
}
/**
* 查找所有的
* @return 返回一个List集合
*/
@RequestMapping("/findAll")
public List<Employee> findAll(){
List<Employee> employeeList = empService.findAllEmployee();
return employeeList;
}
/**
* 根据id进行删除,返回对象ResultMsg
* @param id
* @return
*/
@RequestMapping("deleteEmployee")
public ResultMsg deleteEmployee(@RequestParam Integer id){
return empService.deleteEmployee(id);
}
}
6,jpa的配置文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
7,pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yztc.demo</groupId>
<artifactId>resufuldemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>resufuldemo1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四,在postman中输入地址来进行访问实现增删改查
2](C:\Users\gp\Desktop\知识点\图片\QQ图片20170830202032.jpg)