SpringBoot
Swagger
配置使用注意 Email发送
配置设置复杂邮件简单邮件 qitaqita
SpringBoot Swagger 配置配置swagger时会出现版本问题,经过尝试后,验得SpringBoot 2.5.4与swagger 2.9.2 可适配。
@ApiModel("")
@ApiModelProperty("")
@ApiOperation("")
@ApiParams("")
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("东方")
.select()
//配置要扫描接口的方式
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact("王权", "", "xxxxxxx@qq.com");
return new ApiInfo(
"东方月初",
"Api文档",
"1.0",
"https://www.baidu.com",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
注意
只希望Swagger只在生产环境中使用,在发布的时候不使用
- 判断是不是生产环境: flag = false注入: enable(flag)
//yaml配置文件
spring:
profiles:
active: dev
//swagger配置文件
Profiles profiles = Profiles.of("dev");//开发环境
boolean flag = env.acceptsProfiles(profiles);
.enable(flag);
或者
//yaml配置文件
swagger:
show: true/false
@Value("${swagger.show}")
private boolean swaggerShow;
.enable(swaggerShow);
Email发送
配置设置
spring:
mail:
username: xxxxxxx@qq.com
password: xxxxxxxx
host: smtp.qq.com
properties:
mail.smtp.ssl.enable: true
复杂邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//设置主题
helper.setSubject("hello");
helper.setText("愿你被世界温柔以待", true);
//增加附件
helper.addAttachment("", new File());
helper.setFrom("xxxxx@qq.com");
helper.setTo("xxxxxx@qq.com");
mailSender.send(mimeMessage);
简单邮件
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("");
mailMessage.setText("");
mailMessage.setFrom("");
mailMessage.setTo("");
mailSender.send(mailMessage);
qita
qita
2022/2/7
2022/2/9



