集成swagger

集成swagger

部署配置

在pom文件中添加插件swagger:

    <properties>
        <swagger.version>2.7.0</swagger.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
    </dependencies>

创建swagger配置文件

package com.course.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("myApiListDoc")
                .contact(new Contact("hanzy","xxx","333@qq.com"))
                .description("这是swagger生成的API文档")
                .version("1.0.0")
                .build();
    }


}


给接口添加注解

给类添加注解@Api

@RestController
@Api(value = "/",description = "这是我全部的get方法")
public class MyGetMethod {}

给方法添加注解@ApiOperation

    @RequestMapping(value = "/getCookies",method = RequestMethod.GET)
    @ApiOperation(value = "通过本方法可获取cookie",httpMethod = "GET")
    public String getCookies(HttpServletResponse response){
        //HttpServletRequest 装请求信息类
        //HttpServletResponse 装响应信息类
        Cookie cookie = new Cookie("login","true");
        response.addCookie(cookie);

        return "成功获取cookie";
    }
posted @ 2022-03-09 15:47  77的小白  阅读(67)  评论(0)    收藏  举报