代码拉取完成,页面将自动刷新
同步操作将从 icanci/Java-Review 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
学习目标:
前后端分离
Vue+SpringBoot
后端时代:前端直用管理静态页面;HTML ==> 后端。模板引擎 JSP => 后端是主力
前后端分离时代:
产生一个问题:
解决方案:
在项目中使用Swagger需要springBox
1、新建一个Spring-Web项目
2、导入依赖
<!-- 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>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
3、编写一个Hello工程
4、配置Swagger => Config
@Configuration
@EnableSwagger2
public class SwaggerConfig {
}
5、启动项目就可以看到文档 测试
文档地址: http://localhost:8080/swagger-ui.html
Swagger的bean实例Docket;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置Swagger的bean实例
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger信息 = apiInfo
@Bean
public ApiInfo apiInfo() {
Contact contact = new Contact("icanci", "https://icanci.oschina.io/", "icanci@foxmail.com");
return new ApiInfo(
"JUST-Admin-Web-API",
"Just Do It",
"v1.0",
"https://icanci.oschina.io/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
Docket.select();
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置Swagger的bean实例
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 配置 要扫描接口的方式:RequestHandlerSelectors
// 配置 需要扫描的包:basePackage
// 扫描全部 : any()
// 扫描类上的注解:withClassAnnotation() 注解的反射对象
// 扫描方法上的注解:withMethodAnnotation
.apis(RequestHandlerSelectors.basePackage("cn.icanci.swagger.controller"))
// 过滤路径:paths()
.paths(PathSelectors.ant("/icanci/**"))
.build();
}
//配置Swagger信息 = apiInfo
@Bean
public ApiInfo apiInfo() {
Contact contact = new Contact("icanci", "https://icanci.oschina.io/", "icanci@foxmail.com");
return new ApiInfo(
"JUST-Admin-Web-API",
"Just Do It",
"v1.0",
"https://icanci.oschina.io/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList<VendorExtension>());
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置Swagger的bean实例
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 是否启用Swagger:enable 如果为false 就不能再浏览器中访问
.enable(false)
.select()
// 配置 要扫描接口的方式:RequestHandlerSelectors
// 配置 需要扫描的包:basePackage
// 扫描全部 : any()
// 扫描类上的注解:withClassAnnotation() 注解的反射对象
// 扫描方法上的注解:withMethodAnnotation
.apis(RequestHandlerSelectors.basePackage("cn.icanci.swagger.controller"))
// 过滤路径:paths()
// .paths(PathSelectors.ant("/icanci/**"))
.build();
}
}
我只希望我的Swagger在成产环境使用,在发布的时候不用?
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//配置Swagger的bean实例
@Bean
public Docket docket(Environment environment) {
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev");
//获取项目的环境:
boolean res = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 是否启用Swagger:enable 如果为false 就不能再浏览器中访问
.enable(res)
.select()
// 配置 要扫描接口的方式:RequestHandlerSelectors
// 配置 需要扫描的包:basePackage
// 扫描全部 : any()
// 扫描类上的注解:withClassAnnotation() 注解的反射对象
// 扫描方法上的注解:withMethodAnnotation
.apis(RequestHandlerSelectors.basePackage("cn.icanci.swagger.controller"))
// 过滤路径:paths()
// .paths(PathSelectors.ant("/icanci/**"))
.build();
}
}
// .groupName("A"); 配置多个分组,就是配置多个Docket实例就可
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket1() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("A");
}
@Bean
public Docket docket2() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("B");
}
@Bean
public Docket docket3() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("c");
}
}
只要ModelAndView返回实体类对象即可生成映射
@ApiModel("用户实体类")
@Component
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("用户密码")
private String password;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
}
@RestController
public class HelloController {
@GetMapping(value = "/hello")
public String hello() {
return "hello";
}
//只要我们的接口中存在实体类就有
@GetMapping(value = "/user")
public User user() {
return new User("hello", "pwe");
}
@ApiOperation("hello2控制器")
@GetMapping(value = "/hello2")
public String hello2(@ApiParam("用户名") String username) {
return "hello"+username;
}
}
1、我们可以实现通过Swagger给一些比较难以理解的属性或者接口,增加注释信息
2、接口文档试试更新
3、可以在线测试
Swagger是一个优秀的工具
注意点:在正式项目上线的时候,关闭Swagger!
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。