赞
踩
典型前后端分离:Vue + SpringBoot
后端时代:前端只用管理静态页面,html ==> 后端,模板引擎 jsp ==> 后端是主力
前后端分离时代:
产生一个问题:
Swagger号称世界上最流行的API框架,RestFul API 文档在线自动生成工具 => API文档与API定义同步更新,可以直接运行,在线测试API接口,支持多种语言:java,php
新建一个springboot web项目
导入相关依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <!--或者--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
编写一个Hello工程
创建SwaggerConfig,开启swagger
@Configuration
//@EnableSwagger2 //开启Swagger2, 3.0版本以下
@EnableOpenApi //开启Swagger 3.0版本
public class SwaggerConfig {
}
测试运行
3.0版本前http://localhost:8080/swagger-ui.html
3.0版本http://localhost:8080/swagger-ui/index.html
@Configuration @EnableOpenApi //开启Swagger public class SwaggerConfig { //配置了swagger的docket的bean实例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //是否启动swagger,为false则swagger不能在浏览器中访问 .enable(false) .select() //配置扫描接口 //RequestHandlerSelectors:配置要扫描接口的方式 //basePackage:指定要扫描的包 //RequestHandlerSelectors.any()扫描全部,一般不用 //RequestHandlerSelectors.none()都不扫描 //RequestHandlerSelectors.withClassAnnotation(GetMapping.class)只扫描被注解注解过的类 //RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)通过方法注解扫描 .apis(RequestHandlerSelectors.basePackage("com.study.controller")) //paths() 过滤的路径 .paths(PathSelectors.ant("/study/**")) .build(); } //配置swagger信息:apiInfo,两种方法 private ApiInfo apiInfo(){ /*return new ApiInfo("++ swagger 日记", "喵", "1.0", "http://localhost:8080/", new Contact("study",null,"123@qq.com"), //作者信息 "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());*/ return new ApiInfoBuilder() .title("++ swagger 接口测试") .version("1.0") .build(); } }
我只希望我的swagger在生产环境中使用,在发布的时候不使用,怎么做?
判断是不是生产环境 flag = false
注入enable(flag)的值
//配置了swagger的docket的bean实例 @Bean public Docket docket(Environment environment){ //设置要启动swagger的环境 Profiles profiles = Profiles.of("dev"); //获取项目的环境,判断是否处于自己设定的环境中 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //是否启动swagger .enable(flag) .select() .apis(RequestHandlerSelectors.basePackage("com.study.controller")) .build(); }
配置API文档的分组
在Docket对象中
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//分组
.groupName("pp")
如何配置多个组
//多建几个Docket对象
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Api:用在controller类,描述API接口
@ApiOperation:描述接口方法
@ApiModel:描述对象
@ApiModelProperty:描述对象属性
@ApiImplicitParams:描述接口参数
@ApiResponses:描述接口返回值
@ApiIgnore:忽略接口方法
例子
@Api(tags = "Hello控制类")
@RestController
public class HelloController {
@ApiModel("用户实体类") //文档注释
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
编写需要测试的请求
@GetMapping("/hello")
public User user(User user) {
return user;
}
User类
@ApiModel("用户实体类") //文档注释
public class User {
//这个注释名会被当做测试的表单input的name属性,所以controller参数直接传值User对象
//或者把这里的注释名跟controller方法参数名设置相同
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
进入swagger管理界面http://localhost:8080/swagger-ui/index.html
然后执行,就能看到测试结果
总结:
在启动器中开启springboot自带的异步功能
@SpringBootApplication
@EnableAsync //开启异步任务功能
public class Springboot9TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot9TestApplication.class, args);
}
}
在需要异步的方法上添加注解
@Service
public class AsyncService {
@Async //告诉spring这是一个异步的方法
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理中。。。");
}
}
在启动器中开启springboot自带的定时功能
@SpringBootApplication
@EnableScheduling //开启定时功能
public class Springboot9TestApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot9TestApplication.class, args);
}
}
在需要定时执行的方法上添加注解
@Service
public class ScheduledService {
//在一个特定的时间执行这个方法
//秒 分 时 日 月 周几
//30 0/5 10,18 * * ? 每天10点和18点,每隔5分执行一次
@Scheduled(cron = "0/5 * * * * 0-7") //需要放入cron表达式
public void hello(){
System.out.println("hello, 你被执行了");
}
}
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.4.5</version>
</dependency>
编写配置
spring:
mail:
username: 835241124@qq.com
password: svcgwlpbhtqpbbfc
host: smtp.qq.com
# 开启加密验证,QQ专用
properties: {mail.smtp.ssl.enable: true}
编写测试
@SpringBootTest class Springboot9TestApplicationTests { @Autowired JavaMailSenderImpl javaMailSender; @Test void contextLoads() { //一个简单的邮件 SimpleMailMessage message = new SimpleMailMessage(); message.setSubject("通知"); //标题 message.setText("你好"); //正文 String[] strings = new String[2]; //给两个人发送 strings[0]="xxx@qq.com"; strings[1]="xxx@qq.com"; message.setTo(strings); //接收方 message.setFrom("xxx@qq.com"); //发送方 javaMailSender.send(message); } }
复杂邮件
@Test void contextLoads2() throws MessagingException { //一个复杂的邮件 MimeMessage mimeMessage = javaMailSender.createMimeMessage(); //组装 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); //true:支持多文件 helper.setSubject("通知"); //得先执行setText再执行addInline helper.setText("<h1 style=\"color:red\">你好</h1><img src=\"cid:123\">", true); //true:支持html解析 helper.addInline("123",new File("C:\\Users\\Done\\Desktop\\111.png")); //附件 //helper.addAttachment("111.png",new File("C:\\Users\\Done\\Desktop\\111.png")); helper.setTo("835241124@qq.com"); helper.setFrom("835241124@qq.com"); javaMailSender.send(mimeMessage); }
最后,感谢狂神说
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。