Introduction to the 1.RESTful API
RESTful API is a set of more mature Internet application of the API design theory; Get represents a query, post represents new, delete represents a deletion, put represents a modification; The four request methods have clearly illuminated their interface hole cards: What logic this interface is going to implement. The application of 2.RESTful API in Springboot
①pom.xml Dependency
<?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.didispace</groupid> <artifactId>Chapter3-1-1</artifactId> <version>1.0.0</version > <packaging>jar</packaging> <name>Chapter3-1-1</name> <description>restful API project</description> <parent> <groupId>org.springframework.boot</groupId> <ar Tifactid>spring-boot-starter-parent</artifactid> <version>1.4.3.RELEASE</version> </pare Nt> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &
Lt;java.version>1.8</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> &L T;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid > <scope>test</scope> </dependency> <dependency> <groupId> Org.springframework.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </ dependency> </dependencies> <build> <plugins> <plugin> &L T;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifacti d> </plugin> </plugins> </build> </project>
Details of ② request method
Query Request:
/**
* Company ID get company Information
CompanyID Company ID
*
/@GetMapping ("/getcompanyinfo/{companyid}")
Public Resultinfo Getconame (@PathVariable ("CompanyID") Integer CompanyID) {
date lastday = new Date (new Date (). GetTime () -24*60*60*1000);
map<string,object> map = Maps.newhashmap ();
Company Company = Companyservice.selectbyprimarykey (CompanyID);
Map.put ("Name", Company.getcompanyname ());//Enterprise name
map.put ("icon", Company.getthumbnails ());//thumbnail
map.put ("Staffnum", Companyservice.getstaffnum (CompanyID));//Get the number of users
map.put ("Workshopnum", Companyservice.deptcount (CompanyID)); /Get Department number
map.put ("Lastdaynum", Companyservice.singupnum (Companyid,lastday));//number of users yesterday
return Success (map);
}
New request:
/**
* Add roles
role
@return
*
/@PostMapping ("/addrole")
public Resultinfo addrole (role role) {
int i = rolegroupservice.addrole (role);
Return i>0?success ("Add Role succeeded"): Success ("Add role Failed");
}
Delete request:
Delete Employee
@DeleteMapping ("/delete/{id}") Public
Resultinfo Deletebyprimarykey (@PathVariable ("id") Long ID) {
return (Staffservice.deletebyprimarykey (ID) > 0)? success ("Delete succeeded"): Success ("delete failed");
}
To modify a request:
@RequestMapping (value= "/{id}", method=requestmethod.put) public
String Putuser (@PathVariable Long ID, @ Modelattribute User user) {
//processing "/users/{id}" put request to update user information
user U = users.get (id);
U.setname (User.getname ());
U.setage (User.getage ());
Users.put (ID, u);
Return "Success";
}
Of course
@RequestMapping (value= "/{id}", Method=requestmethod.put)
can be used
@PutMapping ("/{id}")
Alternative, the other way of requesting the same. @ParaVariable annotations have passed the parameters in {} to the variable of the annotation declaration, which is easy to use and at a glance.
All right, I'm Zhang Xing Zhaoyuan. Welcome to the BO Master Technical discussion Group, Group No.: 313145288