狂神swagger:(13条消息) 最详细狂神swagger学习笔记_中文很快乐的博客-CSDN博客_swagger笔记
第五步加了配置输入http://localhost:8080/swagger-ui.html没跳到页面的话在yml文件加上配置
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.配置类
package com.ahead.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //开始swagger
public class SwaggerConfig {
// 可以有多个分组,设置不同的接口权限
@Bean
public Docket docket1(Environment environment){
Profiles profiles = Profiles.of("dev", "test");
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("分组1")
.enable(flag)
.select()
.build();
}
// 配置了swagger的Docket的bean实例
@Bean
// @Profile({"dev","test"})
public Docket docket(Environment environment){
//设置要显示的Swagger环境(或者用上面的 @Profile注解)
Profiles profiles = Profiles.of("dev", "test");
//通过environment.acceptsProfiles判断是否处在设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
System.out.println(flag);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("张三")
//enable是否启动swagger,如果位false,则swagger不能在浏览器中访问
.enable(flag) //输出😱 Could not render e, see the console.
.select()
// RequestHandlerSelectors,配置要扫描接口的方式
// basePackage:指定要扫描的包
// any():扫描全部
// none():不扫描
.apis(RequestHandlerSelectors.basePackage("com.rong.springboot_swagger.controller"))
// 过滤路径(只能扫描到/rong 路径下的)
// .paths(PathSelectors.ant("/rong/**"))
.build();
}
// 配置Swagger信息 = apiInfo
private ApiInfo apiInfo(){
// 作者信息
Contact contact = new Contact("荣", "https://www.baidu.com", "806451906@qq.com");
return new ApiInfo(
"荣哥的SwaggerAPI文档",
"好好学Java",
"1.0",
"https://www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
可以给实体类加注解
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
private String name;
@ApiModelProperty("密码")
private String password;
也可以给控制层接口加注解
@ApiOperation("Hello控制类")
@PostMapping("/user1")
public String hello2(String name,String password){
return "hello"+name;
}
Mybatis-Plus代码生成器
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
package com.example.utils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.Collections;
public class Generator {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://localhost:3306/shirotest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai", "root", "123456")
.globalConfig(builder -> {
builder.author("荣") // 设置作者
.enableSwagger() // 开启 swagger 模式
.fileOverride() // 覆盖已生成文件
.outputDir("D:\\ideaPractice\\MyTest\\springboot_shirotest\\src\\main\\java"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com") // 设置父包名
.moduleName("example") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D:\\ideaPractice\\MyTest\\springboot_shirotest\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude("user")
// .addTablePrefix("wl_","all_")
.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl")
.entityBuilder()
.enableLombok()
.logicDeleteColumnName("deleted")
.enableTableFieldAnnotation()
.controllerBuilder()
.formatFileName("%sController")
.enableRestStyle()
.mapperBuilder()
.enableBaseResultMap() //生成通用的resultMap
// .superClass(BaseMapper.class)
.formatMapperFileName("%sMapper")
.enableMapperAnnotation()
.formatXmlFileName("%sMapper");
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}