1、pom文件引入swagger相关插件
<!--swagger2markup插件-->
<plugin>
<groupId>io.github.swagger2markup</groupId>
<artifactId>swagger2markup-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<!-- api-docs访问url -->
<swaggerInput>http://localhost:8080/zx/swagger?group=zx</swaggerInput>
<!-- 生成为单个文档,输出路径
<outputFile>src/main/doc/api</outputFile>-->
<!-- 生成为多个文档,输出路径 -->
<outputDir>F:downloadswagger</outputDir>
<config>
<!-- wiki格式文档 -->
<!--<swagger2markup.markupLanguage>CONFLUENCE_MARKUP</swagger2markup.markupLanguage> -->
<!-- ascii格式文档 -->
<swagger2markup.markupLanguage>ASCIIDOC</swagger2markup.markupLanguage>
<!-- markdown格式文档 -->
<!--<swagger2markup.markupLanguage>MARKDOWN</swagger2markup.markupLanguage>-->
<swagger2markup.pathsGroupedBy>TAGS</swagger2markup.pathsGroupedBy>
</config>
</configuration>
</plugin>
<!--asciidoctor插件-->
<plugin>
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
<version>1.5.6</version>
<configuration>
<sourceDirectory>F:downloadswagger</sourceDirectory>
<outputDirectory>F:downloadswaggerhtml</outputDirectory>
<backend>html</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<toc>left</toc>
</attributes>
</configuration>
</plugin>
2、 yml文件配置
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
swagger.enabled=true
3、定义配置类
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@ConditionalOnProperty(name = "enabled", prefix = "swagger", havingValue = "true", matchIfMissing = true)
public class Swagger2Config {
@Bean(value = "userApi")
@Order(value = 1)
public Docket groupRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(groupApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springboot.user.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo groupApiInfo() {
return new ApiInfoBuilder()
.title("Swagger")
.description("<div style='font-size:16px;color:red;font-weight:bold'>臻管家2.0-Swagger</div>")
.termsOfServiceUrl("http:///localhost:7012/")
.version("1.0")
.build();
}
}
4、实体类注解使用
@Data
@ApiModel(description = "用户实体类")
public class User implements Serializable {
@ApiModelProperty("主键")
private Integer id;
@ApiModelProperty("账号")
private String name;
@ApiModelProperty("密码")
private String passward;
}
5、控制层注解使用
@RestController
@RequestMapping("/user")
@Api(tags = "用户登录与注册")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("register")
@ApiOperation(value = "测试@RequestParam注解的第一种使用情况", notes = "<span
style='color:red;'>描述:</span> 用来测试@RequestParam注解的第一种使用情况")
@ApiImplicitParams({
@ApiImplicitParam(name = "name",value = "用户姓名",dataType = "String"),
@ApiImplicitParam(name = "passward",value = "用户密码",dataType = "String")
})
public String register(@RequestParam("name")String name,@RequestParam("passward")String
passward) {
boolean i=userService.register(name,passward);
String msg="注册成功";
if(!i){
msg="注册失败";
}
return msg;
}
}
6、启动项目后,访问 http://localhost:7012/swagger-ui.html#/ (7012为应用端的访问ip)