4 springboot集成swagger2

原來(lái)看到別人用swagger,界面挺好看,也方便接口調(diào)試,就自己搭建了一個(gè)

項(xiàng)目還是 http://www.itdecent.cn/p/6f9547e79b2b 這里面的項(xiàng)目,在上面加上了swagger
pom.xml

    <!--swagger2-->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.2.2</version>
    </dependency>

swagger配置文件:

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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
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 createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) .select()
                .apis(RequestHandlerSelectors.basePackage("com.lijia.controller"))//訪問controller目錄
                .paths(PathSelectors.any()) .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("測(cè)試swagger2")
                .description("還是個(gè)孩子呀!")
//                .termsOfServiceUrl("http://localhost:8080/")
                .contact("lijia") .version("1.0") .build();
    }
}

然后再Controller上面添加描述,也可以不添加,

@ApiOperation(value = "測(cè)試swagger2",notes = "測(cè)試swagger2")
@RequestMapping(value = "/swagger",method = RequestMethod.GET)
@ResponseBody
public String swagger(){
    return "helloWorld";
}

啟動(dòng)Application,輸入

http://localhost:8080/swagger-ui.html


如果不指定method的方式,會(huì)全部出現(xiàn),指定了就會(huì)出現(xiàn)一個(gè)。

在下面填入條件,點(diǎn)擊try it out



主要看ResponseCode,如果正常的話是200,自己封裝異常的話就看ResponseBody,比如上面的exception


代碼放在
https://github.com/lijiaccy/springbootmybatis.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容