Spring Boot 接口调试
1. 添加依赖
在 pom.xml 文件中添加
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.4</version>
<type>pom</type> -- 可加可不加
<scope>import</scope> -- 可加可不加
</dependency>
2. 创建配置类
- 创建Docket Bean
- 创建 ApiInfo
package com.example.amanda.base.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Profile("dev")
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig {
/**
* 这里配置swagger扫描的包
* @return
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.amanda.Controller"))
.build()
.groupName("Amanda自学 API");
}
/**
* 创建API的基本信息
* apiInfo() 方法中termsOfServiceUrl 和 contact 可以用 Contact 对象代替
* Contact 中 第一个参数为作者,第二个参数为URL,代替之前的 termsOfServiceUrl,第三个参数为email
*/
private ApiInfo apiInfo() {
// 联系方式
Contact contact = new Contact("Amanda", "http://10.83.1.95", "85455@qq.com");
return new ApiInfoBuilder()
.title("标题标题")
.description("api接口描述")
.contact("854551948@qq.com") -- 可以直接这样写
.contact(contact)
.version("1.0.0")
.build();
}
}
利用 @Configuration 注解,让 Spring来加载该类配置
利用 @EnableSwagger2 注解,启用Swagger2
利用 @Profile("dev"),注明此时为生产环境
如果title 设置为 "标题标题"
整体展示结果
3. WebMvcConfig 接口
package com.example.amanda.base.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.spring.web.SpringfoxWebMvcConfiguration;
@SpringBootApplication
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 静态资源处理
**/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 添加 swagger 文档资源
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
最终的链接为:
http://localhost:8080/doc.html#/SwaggerModels/Amanda%E8%87%AA%E5%AD%A6%20API
4. AuthInterceptor
spring 中的拦截器, 简而言之就是 在一个流程正在进行的时候,你希望干预它的进展,甚至终止它的进行
拦截器可以帮忙验证是否登陆、预先设置数据 以及统计方法等
这里讲解使用 HandleInterceptor , HandleInterceptor是 SpringMVC 项目中的拦截器,拦截目标是请求的地址
5. 修改启动时的Spring Boot Logo
在 resources 下创建一个文件 banner.txt
在线成成文档 [Text to ASCII Art Generator (TAAG) (patorjk.com)]
6. 配置 application.properties
application.properties 配置文件会被Spring Boot自动加载,放到指定位置:
src/main/resources
1. spring.servlet.multipart.max-file-size
最大文件大小
spring.servlet.multipart.max-file-size=10MB
2. spring.servlet.multipart.max-request-size
最大请求大小
spring.servlet.multipart.max-request-size=10MB
3. mybatis.configuration.map-underscore-to-camel-case
将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true
4. spring.profiles.active
- 多种开发环境配置, 首先在pom.xml 文件中添加
#开发/测试/生产环境分别对应dev/test/prod,可以自由定义,当前配置为开发环境
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile></profiles>
- 因为在SwaggerConfig中添加了 @Profile("dev") 因此
#多环境配置
spring.profiles.active=@profileActive@
-
新增两个文件 application-dev.properties | application-prod.properties
其中 application-dev.properties 中添加日志级别
logging.level.springfox.documentation.spring.web.readers.operation=WARN其中 application-prod.properties 中为空
-
环境配置好以后要重启项目...
\