版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/forever_insist/article/details/82999002
Springboot集成 swageer
Springboot集成 swageer
1 引入依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2 编写swagger 配置类
package com.zhangheng.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
@EnableSwagger2
public class Swagger2 {
// swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.zhangheng.controller")).paths(PathSelectors.any())
.build();
}
// 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
// 创建人
.contact(new Contact("hengzhang", "http://www.baidu.com", ""))
// 版本号
.version("1.0")
// 描述
.description("API 描述").build();
}
}
3 再controller 中编写接口文档(主要是一些注解)
package com.zhangheng.controller;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.zhangheng.rabbitmq.ProviderMQ;
import com.zhangheng.rabbitmq.ReceiveMQ;
import io.swagger.annotations.ApiOperation;
@Controller
public class IndexController {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String emailFrom;
/**
* 首页
* @author zhangh
* @date 2018年10月10日下午4:15:14
* @return
*/
@ApiOperation(value = "首页接口", notes = "首页接口文档详情")
@RequestMapping("/index")
public String index() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(emailFrom);
message.setTo(emailFrom);
message.setSubject("this is a subject");
message.setText("this is a text");
mailSender.send(message);
return "index";
}
/**
* 发送消息
* @author zhangh
* @date 2018年10月10日下午4:15:22
* @param message
* @throws IOException
* @throws TimeoutException
*/
@ApiOperation(value = "MQ 发送接口", notes = "发送消息")
@PostMapping("/sendMQ")
public void sendMessage(String message) throws IOException, TimeoutException {
ProviderMQ.sendMQ(message);
}
/**
* 消费消息
* @author zhangh
* @date 2018年10月10日下午4:15:30
* @throws IOException
* @throws TimeoutException
*/
@ApiOperation(value = "MQ 接受消息", notes = "消费消息")
@RequestMapping("/receiveMQ")
public void receiveMQ() throws IOException, TimeoutException {
String str = ReceiveMQ.receive();
System.out.println("str=" + str);
}
}
4 启动
package com.zhangheng;
import java.util.Properties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.github.pagehelper.PageHelper;
/**
* 程序启动入口
*
* @author zhangh
* @date 2018年4月26日上午9:16:27
*/
@SpringBootApplication
@MapperScan("com.zhangheng.dao")
public class Application {
/**
* @author zhangh
* @date 2018年4月28日下午5:18:38
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
// 下面的代码可以自定义banner 还可以控制banner的显示方式(log console off)
// SpringApplication app = new SpringApplication(Application.class);
// app.setBannerMode(Banner.Mode.OFF);
// app.run(args);
}
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "true");
properties.setProperty("dialect", "mysql");
pageHelper.setProperties(properties);
return pageHelper;
}
}
5 访问 http://localhost:10086/springboot/swagger-ui.html
6 补充配置文件 application-dev.yml
# 服务器的端口和contextPath的配置
server:
port: 10086
contextPath: /springboot
# Actuator 监控访问端口 默认值跟上面的端口一样
management:
port: 10087
context-path: /manage
spring:
datasource: # 数据库的配置 用的是阿里的druid连接池
url: jdbc:mysql://localhost:3306/mytest
username: root
password: *******
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,log4j
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
devtools: # 热部署机制 需要依赖一个jar
restart:
enabled: true
mail:
host: smtp.163.com
username: [email protected]
password: *******
# MyBatis 的配置
mybatis:
typeAliasesPackage: com.zhangheng.entity
mapperLocations: classpath:mapper/*.xml
# 日志管理
logging:
level:
com:
zhangheng:
dao: debug
file:
logs/spring-boot-logging.log
# 权限验证
security:
user:
name: iflytek2018
password: iflytek2018
# endpoints 介绍
endpoints:
shutdown:
enabled: false #true 强制下线
info: ##自定义info的id
id: myAppInfo
info:
app:
name: springBootDemo
version: v0.0.1
author: [email protected]
author:
name: zhangheng
email: [email protected]
age: 18
neck: 永远18岁