swagger是一個RESTFUL風(fēng)格的web接口框架 逮京,主要是為了獲取到接口文檔 ,這次使用一個項目簡單測試一下 概说,看看效果
使用步驟:
1:導(dǎo)入依賴:
<!-- 接口文檔的依賴 -->
<!--springfox的核心jar包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>spring-aop</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-context</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>spring-beans</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<!--springfox-ui的jar包(里面包含了swagger的界面靜態(tài)文件) -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2: springmvc配置文件中配置swagger的放行地址
<!--配置下 swagger 的地址放行-->
<mvc:resources location="classpath:/META-INF/resources/" mapping="/swagger-ui.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
3:添加swagger的配置類(雖然是配置類 网梢,最好直接寫到controller層中 )
package com.newcapec.org.controller;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2 //開啟 swagger2
@EnableWebMvc//聲明是 web 項目
@Configuration
public class SwaggerConfig {
@Bean
public Docket api(ApiInfo apiInfo) {
return new Docket(DocumentationType.SWAGGER_2)
.select()
// .apis(RequestHandlerSelectors.any())
.apis(RequestHandlerSelectors.basePackage("com.newcapec.org.controller"))
.build()
.apiInfo(apiInfo);
}
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("衛(wèi)生監(jiān)督管理項目Forest接口文檔")
.description("衛(wèi)生監(jiān)督管理項目Forest接口測試")
.version("1.0.0")
.termsOfServiceUrl("")
.license("衛(wèi)生監(jiān)督管理1.0")
.licenseUrl("http://127.0.0.1:8888/")
.build();
}
}
4:在實體類中的注解配置
@ApiModel(description = "用戶數(shù)據(jù)")
public class Users {
@ApiModelProperty(value = "用戶主鍵,查詢時返回",hidden = true)
private Integer id;
@ApiModelProperty(value = "用戶名字",required = true,example = "TOM")
private String username;
@ApiModelProperty(value = "用戶密碼",required = true,example = "123")
private String password;
@ApiModelProperty(value = "真實名字",required = false,example = "湯姆")
private String realname;
private List<Integer> rolesList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public List<Integer> getRolesList() {
return rolesList;
}
public void setRolesList(List<Integer> rolesList) {
this.rolesList = rolesList;
}
}
5:在控制器中的注解配置
package com.newcapec.org.controller;
import com.newcapec.org.entity.Users;
import com.newcapec.org.service.UsersService;
import com.newcapec.org.utils.ResponseJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
* 異步交互技術(shù)
* @RestController = @Controller + @ResponseBody
*
* 在異步交互技術(shù)的處理器中震缭,所有的返回值為ResponseJson對象
*/
@RestController
@RequestMapping("/users")
@Api(tags = "用戶管理模塊" )
public class UsersController {
@Autowired
private UsersService usersService;
@PostMapping("/add")
@ApiOperation("添加用戶")
public ResponseJson add(@RequestBody Users entity){
usersService.add(entity);
return ResponseJson.success();
}
@PutMapping("/edit")
@ApiOperation("修改用戶")
public ResponseJson edit(@RequestBody Users entity){
usersService.edit(entity);
return ResponseJson.success();
}
@DeleteMapping("/remove/{id}")
@ApiOperation("刪除用戶")
public ResponseJson remove(@PathVariable Integer id){
usersService.remove(id);
return ResponseJson.success();
}
@DeleteMapping("/remove")
@ApiOperation("批量刪除用戶")
public ResponseJson removeBatch(Integer[] id){
usersService.removeBatch(id);
return ResponseJson.success();
}
@GetMapping("/find/{id}")
@ApiOperation("通過id查詢用戶")
public ResponseJson findById(@PathVariable Integer id){
Users users = usersService.findById(id);
return ResponseJson.success(users);
}
@GetMapping("/find")
@ApiOperation("分頁查詢用戶")
public ResponseJson find(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
Users entity){
Map<String,Object> map = usersService.find(pageNum, pageSize, entity);
return ResponseJson.success(map);
}
@GetMapping("/findAll")
@ApiOperation("查詢所有用戶")
public ResponseJson findAll(){
return ResponseJson.success(usersService.findAll());
}
/**
* 登錄
* 1.根據(jù)用戶username查詢到單條記錄
* 2.判斷用戶對象是為null
* 3.如果為空表示用戶名不匹配:給用戶提示(響應(yīng)用戶名有誤的提示)
* 4.如果不為空表示用戶名匹配:
* 5.判斷密碼是否匹配:用戶提交的密碼與查詢到密碼進(jìn)行比對
* 6.比對成功表示用戶登錄成功:將用戶信息存放在session中
* 7.比對不成功表示用戶提供的密碼有誤:給用戶提示(響應(yīng)密碼有誤的提示)
*/
@PostMapping("/login")
@ApiOperation("用戶登陸")
public ResponseJson login(String username, String password, HttpSession session){
Users users = usersService.findByUsername(username);
if(users == null){
return ResponseJson.error(501, "用戶名有誤");
}
if(users.getPassword().equals(password)){
session.setAttribute("loginUser", users);
return ResponseJson.success();
}
return ResponseJson.error(501, "密碼有誤");
}
@GetMapping("/logout")
@ApiOperation("用戶登出")
public ResponseJson logout(HttpSession session){
session.removeAttribute("loginUser");
session.invalidate();
return ResponseJson.success();
}
@GetMapping("/exists")
@ApiOperation("用戶是否存在")
public Map<String, Object> exists(String username){
return usersService.exists(username);
}
}
啟動項目 ,直接訪問 http://localhost:8888/swagger-ui.html 查看本次項目的接口文檔