pom.xml添加Swagger2依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
创建Swagger2配置类
package com.springbootweb.common.config.swagger2;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.springbootweb.modules.system.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("maven构建Spring boot项目")
.description("maven构建Spring boot项目 集成mybatis使用pagehelper插件 ,实现热部署 by 陈晨")
.termsOfServiceUrl("http://blog.csdn.net/chen1218chen")
.contact("chenchen")
.version("1.0")
.build();
}
}
controller
rest接口上添加 @ApiOperation标签,即可生成对应的接口文档,文档更详细的配置请查看swagger2 api
package com.springbootweb.modules.system.controller;
import javax.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.springbootweb.common.utils.SysResponse;
import com.springbootweb.modules.system.pojo.TestProperties;
import com.springbootweb.modules.system.pojo.User;
import com.springbootweb.modules.system.service.IUserService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Resource(name = "userService")
private IUserService userService;
@ApiOperation(value="@value注解的方式获取配置文件信息", notes="@value注解的方式获取配置文件信息")
@GetMapping("/test")
@ResponseBody
public String test(){
log.info("------------sdss");
return name;
}
@ApiOperation(value="封装成对象的方式获取配置文件信息", notes="封装成对象的方式获取配置文件信息")
@GetMapping("/test1")
@ResponseBody
public String test1(){
return testProperties.getName()+"========="+testProperties.getAge();
}
@ApiOperation(value="登陆", notes="登陆")
@PostMapping("/login")
public SysResponse login(@RequestParam String username, String password) {
Subject subject = SecurityUtils.getSubject();
if (!subject.isAuthenticated()) {
return SysResponse.fail("登录失败,用户名或密码不正确!");
}
String principal = (String) subject.getPrincipal();
if (username.equals(principal)) {//同一用户登录
return SysResponse.ok("登录成功");
}
subject.logout();//否则,原用户退出登录,新用户登录
try {
subject.login(new UsernamePasswordToken(username, password));
return SysResponse.ok("登录成功");
} catch (AuthenticationException e) {
e.printStackTrace();
return SysResponse.fail("登录失败,用户名或密码不正确!");
}
}
@ApiOperation(value="退出", notes="退出")
@GetMapping("/logout")
public boolean logout() {
Subject subject = SecurityUtils.getSubject();
if (subject.isAuthenticated()) {
subject.logout();
}
return true;
}
@GetMapping("/unAuthenticated")
public SysResponse unAuthenticated() {
return SysResponse.fail("Haven't logined");
}
@ApiOperation(value="获取当前登陆用户", notes="获取当前登陆用户")
@GetMapping("/getCurrentUser")
public User getCurrentUser() {
Subject subject = SecurityUtils.getSubject();
if (!subject.isAuthenticated()) {
return null;
}
String username = (String) subject.getPrincipal();
return userService.getUserByName(username);
}
@GetMapping("/port")
public String port() {
return port;
}
}
完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html