当前位置:首页 >> 开发者生态 >> 【SpringBoot】SpringBoot配置Swagger,索尼xperia z1评测

【SpringBoot】SpringBoot配置Swagger,索尼xperia z1评测

cpugpu芯片开发光刻机 开发者生态 1
文件名:【SpringBoot】SpringBoot配置Swagger,索尼xperia z1评测 【SpringBoot】SpringBoot配置Swagger

文章目录 前言配置步骤使用步骤总结

前言

使用Swagger只需要按照规范去定义接口及接口的相关信息,就可以做到生成接口文档和在线接口调试页面 官网:Swagger官网 Knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案


配置步骤

1.导入knife4j的maven坐标(测试需要导入web依赖哦~)

<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</version></dependency>

2.再配置类中加入knife4j相关配置

3.设置静态资源映射,否则接口文档页面无法访问

package com.config;import com.sky.interceptor.JwtTokenAdminInterceptor;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;/*** 配置类,注册web层相关组件*/@Configuration@Slf4j //日志门面public class WebMvcConfiguration extends WebMvcConfigurationSupport {/*** 通过knife4j生成接口文档* 我这里提供了日志功能,不需要的可以删除* 这里是第二步*/@Beanpublic Docket docket() {log.info("开始注册knife4j接口文档..."); //输出日志ApiInfo apiInfo = new ApiInfoBuilder().title("XXX接口文档").version("XXX").description("XXX接口文档").build();Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo).select()//扫描规则,当前包及其子包.apis(RequestHandlerSelectors.basePackage("扫描包的路径名")) .paths(PathSelectors.any()).build();return docket;}/*** 设置静态资源映射* 这里是第三步,设置访问页面*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("开始注册静态资源映射...");//在这里配置访问页面,不修改访问路径为:localhost:8080/doc.htmlregistry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}}

4.访问localhost:8080/doc.html,即可访问成功

使用步骤

使用Swagger只需要再类上添加注解即可

可以添加这些注解,为里面的属性赋值,得到的页面会有信息提示,提高可读性

总结

本文我们学会了如何配置Swagger来对后端接口进行测试,其中配置的代码不需要会写,只需要更改为自己的即可

协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接