RestFull api Interface
Separate front and rear ends of the interface specification developed
What is RestFull directory is more popular api design specifications
NOTE: restfull api specification application scenario, the front end of the separation project
Data field interface, such as:
/ Users / 999 to 999 to obtain ID information
/ Users / list to get all of the user information
/ Users / add this page to open the
/ Users / save new data
/ Users / edit to open the modified pages
/ Users / save form has a value determined according to the primary key for updates
/ Users / del / 999 Delete id 999 information
Restfull api-style interface to differentiate operated by different request
get / users / 999 to obtain information id 999
get / users get all user information
post / users new record
put / users modify the information
patch / users incremental changes
delete / users / 999 Delete id 999 information
How to create a restfull style data interface
Note: springmvc have very good support for restfull style api
Style follows
Package com.seecen.sc1904springboot.controller;
Import com.seecen.sc1904springboot.pojo.User;
Import org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.PathVariable;
Import org.springframework.web.bind .annotation.RequestMapping;
Import org.springframework.web.bind.annotation.RequestMethod;
Import org.springframework.web.bind.annotation.ResponseBody;
Import java.util.ArrayList;
Import java.util.List;
/ **
* GET / users / 999 to 999 to obtain information id
* get / users get all user information
* post / users new record
* put / users modify the information
* Patch / users incremental changes
* delete / users / 999 to delete the information id 999
* /
@Controller
@ RequestMapping ( "the Users" )
public class the UserController {
// GET the Users / 9999
@ RequestMapping (value = "/ {id } ", Method = RequestMethod.GET)
@ResponseBody
public the User getUserById (@PathVariable (" id " ) Integer id) {
// persistence operations: Gets the id and returns the records based on
the User User = new new the User ();
user.setUserId (ID);
user.setUserName ( "John Doe" );
return User;
}
//get / users get all user information
@RequestMapping (value = "", Method = RequestMethod.GET)
@ResponseBody
public List <the User> GetAllUsers () {
List <the User> List = new new the ArrayList <> ();
the User User = new new the User ();
user.setUserId ( . 1 );
user.setUserName ( "John Doe" );
List.add (User);
return ; List
}
// new post / users record
@RequestMapping (value = "", method = RequestMethod.POST)
@ResponseBody
public User addNewUser(User user){
//新增一条记录并获取user对象
return user;
}
// put /users 修改信息
@RequestMapping(value = "",method = RequestMethod.PUT)
@ResponseBody
public User updateUser(User user){
return user;
}
//patch /users 增量的改
@RequestMapping(value = "",method = RequestMethod.PATCH)
@ResponseBody
public User patchUser(User user){
return user;
}
//delete /users/999
@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
@ResponseBody
public User del(@PathVariable("id") Integer id){
return new User();
}
}
Postman test
Swaggerui framework test
- Introducing dependencies
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
2.编写配置文件(JAVA类的方式进行配置)
Java类来管理bean对象
通过@Bean注解来管理bean对象 (必须要配置)
package com.seecen.sc1904springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
//@Configuration 就个类是一个spring框架的配置文件
//spring框架的配置文件主要体现的是创建什么bean对象
@Configuration//spring配置文件,xml, java类来体现配置信息
@EnableSwagger2
public class Swagger2 {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors
.basePackage("com.seecen.sc1904springboot.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("Spring Boot中使用Swagger2构建RESTful APIs")
.termsOfServiceUrl("http://www.geek5.cn")
.contact(new Contact("calcyu","http://geek5.cn","[email protected]"))
.version("1.0")
.build();
}
}
注解
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性
注意:@ApiImplicitParam的参数说明
控制层controller(上面Swagger2类中 这个包下所有控制层的方法都会获取)
package com.seecen.sc1904springboot.controller;
import com.seecen.sc1904springboot.pojo.RESTfullResult;
import com.seecen.sc1904springboot.pojo.User;
import com.seecen.sc1904springboot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController // 返回的所有都是json的
@RequestMapping("/user")
@Api("用户的增删改查功能")
public class UserController2 {
@Autowired
private UserService userService;
// emp/insert获取用户信息 @PathVariable("empno")Integer empno
@GetMapping("/{id}")
@ApiOperation("根据id主键返回用户信息")
@ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "json")
public RESTfullResult<User> getAllUsers(@PathVariable("id")Integer id) {
User list = userService.selectByPrimaryKey(id);
return RESTfullResult.success(list);
}
}
项目开始运行了 访问测试地址:http://项目实际地址/swagger-ui.html
然后就可以对控制层的所有方法进行测试了
测一个添加方法吧
执行后
执行成功了
数据库看看
ok
这就是Swaggerui框架测试