<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>档缺
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public DocketcreateRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//controller所在包
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger")//文档标题
.description("SpringBoot整合Mybatis的CRUD *** 作")//文档描述
灶蠢蠢 .version("1.0")//文档版本
隐陪 .build())
}
}
http://localhost:8080/swagger-ui.html
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务,它有着如下的优点:
1)及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
2)规范性 (并且保证接口的规范性,如接口的地址,请求渗行方式,参数及响应格式和错误信息)
3)一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
4)可测性 (直接在接口文档上进行测试,以方便理解业务)
1)添加pom.xml依赖
<pre class="md-fences md-end-block md-fences-with-lineno" lang="xml" contenteditable="false" cid="n252" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial"启姿>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency></pre>
2)创建swagger配置文件
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java"丛旁哗 contenteditable="false" cid="n255" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
@Configuration
@EnableSwagger2
public class Swagger2Config {
}</pre>
3)启动程序
访问路径: http://localhost:8081/swagger-ui.html ,出现生成的文档页面。
此为原生界面,很难看,所以我们需要引入swagger-bootstrap-ui,也就是Knife4j的前身
4)使用Knife4j
a)添加pom.xml依赖
<pre class="md-fences md-end-block md-fences-with-lineno" lang="xml" contenteditable="false" cid="n268" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency></pre>
b)开启Swagger2配置
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n271" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
import java.util.ArrayList
import java.util.List
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.apiInfo(apiInfo())
.select()
//swagger要扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.dsblog.server.controller"))
.paths(PathSelectors.any())
.build()
}
b)重启服务
访问地址: http://localhost:8081/doc.html ,这个ui界面看起来就更美观,更符合国人的使用习惯
1、添加pom.xml依赖
<pre class="md-fences md-end-block md-fences-with-lineno" lang="xml" contenteditable="false" cid="n282" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=" http://maven.apache.org/POM/4.0.0 " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ">
<modelVersion>4.0.0</modelVersion>
</project></pre>
2、创建Swagger2Config配置文件
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n285" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
package com.dsblog.server.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.*
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spi.service.contexts.SecurityContext
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2
import java.util.ArrayList
import java.util.List
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.apiInfo(apiInfo())
.select()
//swagger要扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.dsblog.server.controller"))
.paths(PathSelectors.any())
.build()
.securityContexts(securityContexts())
.securitySchemes(securitySchemes())
}
}</pre>
3、创建SecurityConfig配置文件
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n288" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
package com.dsblog.server.config.security
import com.dsblog.server.model.User
import com.dsblog.server.service.IUserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.builders.WebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
}</pre>
4、创建测试Controller,其中/login登录和/logout退出方法不需要Authorize验证,和上面的重写方法<mark style="box-sizing: border-boxbackground: rgb(255, 255, 0)color: rgb(0, 0, 0)">configure(WebSecurity web)</mark>匹配,user/info方法需要Authorize验证才能进行访问
<pre class="md-fences md-end-block md-fences-with-lineno" lang="java" contenteditable="false" cid="n291" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: monospace, monospacefont-size: 0.9emwhite-space: predisplay: blockbreak-inside: avoidtext-align: leftbackground-image: background-position: var(--code-block-bg-color)background-size: background-repeat: var(--code-block-bg-color)background-attachment: background-origin: background-clip: background-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6px 0pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: normalletter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">
package com.dsblog.server.controller
import com.dsblog.server.model.ResultBean
import com.dsblog.server.model.User
import com.dsblog.server.model.UserLoginParam
import com.dsblog.server.service.IUserService
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import java.security.Principal
@Api(tags = "LoginController")
@RestController
public class LoginController {
}</pre>
5、重启程序,输入 http://localhost:8081/doc.html
6、测试
5)携带token发送到后台获取用户信息,验证通过
1、在Nuget中安装Swashbuckle,它会依赖颤搭安装Swashbuckle.Core。安装完成之后,会在茄含拿你的App_Start目录下增加SwaggerConfig.cs文件。2、右键你的Web API项目,选择属性,然后点击老判生成。勾选XML文档文件。上面输出路径自己选好。这是我的配置方式
3、在SwaggerConfig.cs文件中,取消c.IncludeXmlComments(GetXmlCommentsPath())的注释,并为该方法写代码,其实就是查找第二部生成的XML文件
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)