maven集成swagger

一、先建立swagger的java文件

package com.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;

//让spring来加载该类

@Configuration

//启动swagger2
@EnableSwagger2

//引入配置类
@EnableWebMvc
public class SwaggerConfig {

    @Bean
    public Docket buildDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(buildApiInf())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com"))//controller路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo buildApiInf(){
        return new ApiInfoBuilder()
                .title("接口列表")
                .termsOfServiceUrl("http://127.0.0.1:8080/swagger-ui.html")
                .description("springmvc swagger 接口测试")
                .version("1.0.0")
                .build();
    }

}


二、在spring-mvc.xml文件下加上swagger引用

//引入的swagger在项目中的实际位置

<mvc:resources mapping="swagger-ui.html**" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**"
        location="classpath:/META-INF/resources/webjars/" />

三、实体类加上注解

//配置实体类名

@ApiModel(value="Architecture")

//实体类参数(value:swagger显示的属性名,hidden:可以理解为是否隐藏)

@ApiModelProperty(value="ID",hidden=true)

四、在ctroller层添加注解

//swagger上显示的接口名与备注名(description设置备注名,设置备注名的参数还有tags,这个我设置之后地址就不对了)

@Api(value="Architecture",description="设置建筑数据")

//controller与restcontroller的区别是,controller默认返回String是返回到该String的页面,需要返回数据需要加@requestbody注解,而restcontroller是默认返回数据
@RestController

//调用该方法的url地址
@RequestMapping("/architecture")

//swagger上显示的方法名

@ApiOperation(value = "插入建筑数据", notes = "插入建筑数据")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值