springboot整合swagger+knife4j生产Api文档

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>&nbsp;用来测试@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)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值