1.在pom中引入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>
2.新建swagger2配置类
package best.yuwb.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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Created by venbill on 2017/5/17.
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//扫描controller所在包
.apis(RequestHandlerSelectors.basePackage("best.yuwb.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Blog4g Rest API")
.description("love coding,not only coding")
.termsOfServiceUrl("http://yuwb.pub/")
.contact("yuwb")
// 鱼尾巴
.version("1.0")
.build();
}
}
3.controller中添加注释
package best.yuwb.controller;
import best.yuwb.model.RtInfo;
import best.yuwb.model.User;
import best.yuwb.service.EmailService;
import best.yuwb.service.UserService;
import best.yuwb.util.CookieOperaiton;
import best.yuwb.util.RandomCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
@Api(description = "用户操作")
@Controller
@RequestMapping("/user")
public class UserController {
private Logger logger = Logger.getLogger(UserController.class);
@Autowired
private UserService userService;
@Autowired
private EmailService emailService;
//注册
@ApiOperation(value="用户注册", notes="录入有效信息,返回rtinfo")
@RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public RtInfo getUserInfo(@RequestBody User registerUser) {
System.out.println("===>in");
RtInfo rtInfo = new RtInfo();
String username = registerUser.getUsername();
String email = registerUser.getEmail();
String password = registerUser.getPassword();
if (StringUtils.isEmpty(username)||StringUtils.isEmpty(email)||StringUtils.isEmpty(password)){
rtInfo.setError_code(1);
rtInfo.setError_msg("输入不能为空!");
return rtInfo;
}
//判断用户名含有非法字符 @
if (username.contains("@")){
rtInfo.setError_code(1);
rtInfo.setError_msg("用户名不能含有字符@ ");
return rtInfo;
}
//判断用户名和邮箱是否被占用
if (!userService.isNameUsed(username)){
rtInfo.setError_code(1);
rtInfo.setError_msg("用户名被占用,请更改用户名!");
return rtInfo;
}
if (!userService.isEmailUsed(email)){
rtInfo.setError_code(1);
rtInfo.setError_msg("邮箱已被注册,请更改邮箱!");
return rtInfo;
}
//生成激活码
String activeCode = RandomCode.getEnCodes(20);
//发送邮件,让用户点击
boolean isSend = emailService.send( email,userService.getActivateContent(username,activeCode));
if (!isSend){
rtInfo.setError_code(1);
rtInfo.setError_msg("邮件没有发送成功,请确认邮箱地址!");
return rtInfo;
}
registerUser.setActiveCode(activeCode);
registerUser.setActiveStatus(0);
registerUser.setCreateTime(new Date());
//保存到数据库
boolean bol = userService.insertUser(registerUser);
//返回
if (bol){
rtInfo.setRt_msg("注册成功!请打开邮件点击链接激活!");
}else {
rtInfo.setError_msg("异常错误!请重新注册!");
}
return rtInfo;
}
//激活
@ApiOperation(value="激活账号", notes="录入有效信息,返回rtinfo")
@RequestMapping(value = "/activateAccount", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public RtInfo activateAccount( @RequestParam(value = "username", required = false, defaultValue = "") String username,@RequestParam(value = "activeCode", required = false, defaultValue = "") String activeCode) {
RtInfo rtInfo = new RtInfo();
if (StringUtils.isEmpty(username)||StringUtils.isEmpty(activeCode)){
rtInfo.setError_code(1);
rtInfo.setRt_msg("请求参数错误,无效验证!");
return rtInfo;
}
boolean bol = userService.activateAccount(username,activeCode,2);
if (bol){
rtInfo.setRt_msg("账户已激活!请登录!");
}else {
rtInfo.setError_code(1);
rtInfo.setRt_msg("账户激活错误!请重试!");
}
return rtInfo;
}
//登录
@ApiOperation(value="用户登录", notes="生成token,存入cookie")
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public RtInfo login(HttpServletResponse response, @RequestParam(value = "username", defaultValue="")String username, @RequestParam(value = "password",defaultValue = "")String password, @RequestParam(value = "savePwd")boolean savePwd){
RtInfo rtInfo = new RtInfo();
if (StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
rtInfo.setError_code(1);
rtInfo.setRt_msg("输入为空,登录无效!");
return rtInfo;
}
String token = userService.login(username,password,savePwd);
if (token.equals("")){
CookieOperaiton.addCookie(CookieOperaiton.cookieDomain,token,savePwd?7:-1,response);
rtInfo.setError_code(0);
rtInfo.setRt_info("登录成功!");
}else {
rtInfo.setError_code(1);
rtInfo.setError_msg("用户名或者密码错误,登录失败!");
}
return rtInfo;
}
//登出
@ApiOperation(value="用户登出", notes="删除cookie")
@RequestMapping(value = "/loginOut", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public RtInfo loginOut(HttpServletRequest request,HttpServletResponse response){
RtInfo rtInfo = new RtInfo();
boolean bol = CookieOperaiton.delCookie(CookieOperaiton.cookieDomain,request,response);
if (bol){
rtInfo.setRt_info("登出成功!");
}else {
rtInfo.setError_code(1);
rtInfo.setError_msg("登出失败!");
}
return rtInfo;
}
//找回密码
@ApiOperation(value="找回密码", notes="发送获取密码html到邮件")
@RequestMapping(value = "/getPassword",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public RtInfo getPassword(){
RtInfo rtInfo = new RtInfo();
return rtInfo;
}
}
4.页面打开 http://localhost:8080/swagger-ui.html#/