SpringBoot swagger2的使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_36511401/article/details/101283899

1、新建项目。

2、pom的配置。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <mybatis-plus-boot-starter.version>2.1.9</mybatis-plus-boot-starter.version>
    <mysql.version>5.1.47</mysql.version>
    <com.alibaba.version>1.2.37</com.alibaba.version>
    <springfox-swagger.version>2.9.2</springfox-swagger.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--swagger-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-swagger.version}</version>
    </dependency>
</dependencies>

3、application.yml的配置

4、swagger的配置。

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zxj.reptile"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restfun风格")
                .version("1.0")
                .build();
    }
}

5、测试类。在类上面添加@Api,在方法上面添加@ApiOperation,如果方法有参数的话,要多加一个ApiImplicitParams。

@Api(value = "demo", tags = {"测试demo"})
@RestController
@RequestMapping("demo")
public class DemoApi {
    @Autowired
    private IDemoService demoService;

    @ApiOperation(value = "添加内容", notes = "添加内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "内容",  dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/insert", method = RequestMethod.PUT)
    public AjaxJson addDemo(@RequestParam(value = "content") String content) {
        AjaxJson ajaxJson = new AjaxJson<>();
        try {
            Demo entity = new Demo();
            entity.setContent(content);
            demoService.insert(entity);
            ajaxJson.setData(entity);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setError("失败: " + e.getMessage());
        }
        return ajaxJson;
    }

    @ApiOperation(value = "获取列表信息", notes = "获取列表信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public AjaxJson listDemo() {
        AjaxJson ajaxJson = new AjaxJson<>();
        try {
            List<Demo> entityList = demoService.selectList(new EntityWrapper<>());
            ajaxJson.setData(entityList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setError("失败: " + e.getMessage());
        }
        return ajaxJson;
    }
}

6、结果展示。

7、总结。

  1、用了swagger之后,测试起来就十分的方便,给其他人调用的话,就可以不写Api文档,直接给他们swagger的地址就可以了。

  2、swagger的地址是:服务器Ip + 服务器端口 + /swagger.ui.html  。

  3、@RequestMapping。如果查询的话,用RequestMethod.GET。如果提交内容的话,用RequestMethod.POST。如果更新内容的话,用RequestMethod.PUT。如果更新部分内容的话,用RequestMethod.PUTCH。如果删除的话,用RequestMethod.DELETE。

有不解的地方,可以在下方留言。

猜你喜欢

转载自blog.csdn.net/qq_36511401/article/details/101283899
今日推荐