springboot 集成 Swagger
1、引入赖
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.0.RELEASE</version>
</dependency>
2、配置
package com.czy.echat.config;
import com.google.common.base.Predicates;
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.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
@EnableSwagger2
public class Swagger2Config {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 指定扫描的包路径来定义指定要建立API的目录。
* @return
*/
@Bean
public Docket coreApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(adminApiInfo())
.groupName("adminApi")
.select()
//只显示admin下面的路径
.paths(Predicates.and(PathSelectors.regex("/.*")))
.build();
}
private ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("聊天系统--api文档")
.description("聊天系统接口描述")
.version("1.0")
.contact(new Contact("余华建","http://baidu.com","yuhuajian2021@163.com"))
.build();
}
}
3、写controller
package com.czy.echat.controller;
import com.czy.echat.dao.LoginInfoDAO;
import com.czy.echat.enums.LoginTypeEnum;
import com.czy.echat.model.*;
import com.czy.echat.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
/**
*登录和注册的控制器
*/
@Controller
@Api(tags = "web-登录接口")
public class LoginController {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private UserService userService; //用户service类
@Autowired
private SimpMessagingTemplate messagingTemplate; //消息模板
@Autowired
private ParticipantRepository participantRepository; //在线用户存储
private static final String SUBSCRIBE_LOGIN_URI = "/topic/login"; //订阅的登录地址
/**
* 反馈前端ajax登录的消息
* @param user
* @return
*/
@RequestMapping(value = "/reply/login", method = RequestMethod.POST)
@ApiOperation(value = "反馈前端ajax登录的消息")
@ResponseBody
public ReplyLoginMessage replayLoginMessage(@RequestBody User user){
if (user.getName() == null || user.getName().trim().equals("")
|| user.getPassword() == null || user.getPassword().equals("")){
return new ReplyLoginMessage(false, ReplyLoginMessage.USER_NAME_OR_PASSWORD_NULL);
}
boolean isExist = userService.isExistUser(user.getName());
if (!isExist){
return new ReplyLoginMessage(false, ReplyLoginMessage.USER_NAME_NOT_EXIST);
}
User res = userService.validateUserPassword(user.getName(), user.getPassword());
if (res == null){
return new ReplyLoginMessage(false, ReplyLoginMessage.USER_PASSWORD_WRONG);
}
return new ReplyLoginMessage(true);
}
/**
* 反馈前端ajax注册的消息
* @param user
* @return
*/
@RequestMapping(value = "/reply/regist", method = RequestMethod.POST)
@ApiOperation(value = "反馈前端ajax注册的消息")
@ResponseBody
public ReplyRegistMessage replyRegistMessage(@RequestBody User user){
boolean isExist = userService.isExistUser(user.getName());
if (isExist){
return new ReplyRegistMessage(false, ReplyRegistMessage.USER_NAME_EXIST);
}
if (user.getPassword() != null){
userService.insertUser(user.getName(), user.getPassword());
}
return new ReplyRegistMessage(true);
}
/**
* 登录进入聊天室
* @param user
* @param request
* @return
*/
@RequestMapping(value = "/chat", method = RequestMethod.POST)
@ApiOperation(value = "登录进入聊天室")
public String loginIntoChatRoom(User user, HttpServletRequest request, Model model){
user = userService.validateUserPassword(user.getName(), user.getPassword());
if (user == null){
return "login";
}
user.setLoginDate(new Date());
user.setPassword(null); //设空防止泄露给其他用户
model.addAttribute("userName",user.getName());
HttpSession session = request.getSession();
session.setAttribute("user", user);
//保存登陆信息
LoginInfoDo loginInfo = LoginInfoDo.builder().userId(user.getId()).userName(user.getName()).
status(LoginTypeEnum.LOGIN.getCode()).createTime(new Date()).build();
userService.addUserLoginInfo(loginInfo);
messagingTemplate.convertAndSend(SUBSCRIBE_LOGIN_URI, user);
participantRepository.add(user.getName(), user);
logger.info(user.getLoginDate() + ", " + user.getName() + " login.");
return "chatroom";
}
/**
* 登录页面
* @return
*/
@RequestMapping(value = {"/", "/index", ""}, method = RequestMethod.GET)
@ApiOperation(value = "控制进入登录页")
public String index(){
return "index";
}
/**
* 返回当前在线人数
* @return
*/
@SubscribeMapping("/chat/participants")
@ApiOperation(value = "登录进入聊天室")
public Long getActiveUserNumber(){
return Long.valueOf(participantRepository.getActiveSessions().values().size());
}
@GetMapping(value = "/reply/getOnLineUserMessage")
@ApiOperation(value = "获取在线用户信息")
@ResponseBody
public List<LoginInfoDo> getOnLineUserMessage(){
return userService.getOnLineUser();
}
}
4、访问
http://localhost:8088/swagger-ui.html
如果想改样式成这样
可以引入
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
类似操作