jeesite集成swagger2

1、在原有的jeesite工程的pom文件中增加swagger依赖

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger2</artifactId>

    <version>2.4.0</version>

</dependency>

<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-swagger-ui</artifactId>

    <version>2.4.0</version>

</dependency>

2、增加swagger的配置类

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

 

import springfox.documentation.builders.ApiInfoBuilder;

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

@EnableWebMvc

@EnableSwagger2

@ComponentScan("com.thinkgem.jeesite.test.web")

public class SwaggerConfig extends WebMvcConfigurationSupport{

 

    @Bean

    public Docket petApi() {

        return new Docket(DocumentationType.SWAGGER_2)

             .groupName("api")

             .select()

             //.paths(regex("/api/.*"))//拦截的接口路径

             .build()

             .apiInfo(apiInfo());

 

    }

 

    private ApiInfo apiInfo() {

        return new ApiInfoBuilder()

             .title("服务平台 API")

             .description("服务")

             .termsOfServiceUrl("http://www.xxx.cn/")

             .contact(new Contact("xxx","http://www.XXXIII.cn/","xxx@qq.com"))

             .version("1.0")

             .build();

    }

}

@ComponentScan指定要扫描的controller的包,

可以指定多个包例如:basePackages = {"com.thinkgem.jeesite.test1.web","com.thinkgem.jeesite.test2.web"}

上面的例子相当于@ComponentScan(basePackages = {"com.thinkgem.jeesite.test1.web"})

3、在spring-mvc.xml的拦截其中增加静态资源访问控制。

<!--静态资源映射 -->

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

4、编写测试controller

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.stereotype.Controller;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;

 

/**

 * 测试Controller

 */

@RestController

@Api(description="测试1controller")

@RequestMapping(value="/test1Controller")

public class Test1Controller {

  

   @RequestMapping(value="/getEq/{a}/{b}", method=RequestMethod.GET)

   @ApiOperation(value="GET例子", notes="GET测试例子", response=String.class)

   public String getEq(@ApiParam(required=true, value="第一个参数")

                   @PathVariable String a,//这个参数必须有

                   @ApiParam(required=true, value="第二个参数")

                   @PathVariable String b){//这个参数必须有

      System.out.println("a================="+a);

      System.out.println("b================="+b);

      return b;

   }

 

   @RequestMapping(value="/postEq", method=RequestMethod.POST)

   @ApiOperation(value="POST例子", notes="POST例子", response=String.class)

   public String postEq(@ApiParam(required=true, name="a", value="第一个参数")

                    @RequestParam(required=true) String a,//这个参数必须有

                    @ApiParam(required=false, name="b", value="第二个参数")

                    @RequestParam(required=false) String b){//这个参数可有可无

      System.out.println("a================="+a);

      System.out.println("b================="+b);

      return b;

   }

}

5、启动项目,在浏览器中访问http://localhost:8080/jeesite/swagger-ui.html即可。

具体的controller中的@Api注解信息,参考

https://github.com/wkennedy/swagger4spring-web

http://www.cnblogs.com/softidea/p/6251249.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值