jeesite中集成Swagger2 三步

jeesite中集成Swagger2 三步

前后端分离时使用swagger来进行接口文档的管理还是很不错的选择。这里记录一下如何在java开源开发平台jeesite上集成swagger2。

一、导入依赖

`<!-- swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.7.0</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.7.0</version>
    </dependency> `  

二、配置类编写

`

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.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;

/**
* 
* @author houzhongfei
 *
*/
@EnableWebMvc  
@EnableSwagger2  
@ComponentScan(basePackages = {"com.ms.plat.modules.api"})  
@Configuration  
public class SwaggerConfig extends WebMvcConfigurationSupport{  

@Bean  
public Docket createRestApi() {  
    return new Docket(DocumentationType.SWAGGER_2)  
            .apiInfo(apiInfo())
            .groupName("project-java-api")
            .select()  
            .apis(RequestHandlerSelectors.basePackage("com.ms.plat.modules.api"))  
            .paths(PathSelectors.any())
            .build();  
}  

private ApiInfo apiInfo() {  
    return new ApiInfoBuilder()  
            .title("project API")  
            .termsOfServiceUrl("http://localhost:8181/project/swagger-ui.html")  
            .contact("JAVA")  
            .version("1.0")  
            .build();  
}  
}  

`
三、在接口中增加swagger相关注解

`package com.ms.plat.modules.api.admin.logsmanage;

import org.springframework.stereotype.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.ResponseBody;

import com.ms.plat.common.web.BaseController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 
 * @author houzhongfei
 * 
 */
@Controller
//@Api(value = "EmailLog API Controller(here is @Api value)", tags = "测试接口(here is @Api tags)")
public class EmailLog extends BaseController {


@ApiOperation(value = "here is @ApiOperation value",tags = "测试接口2(here is @Api tags)", notes = "here is @ApiOperation notes")
@RequestMapping(value = "hello/{name}", method = RequestMethod.POST)
@ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order1111") })
public @ResponseBody String helloSwagger(
        @ApiParam(name = "name", value = "参数名字(here is @ApiParam value)", required = true) @PathVariable String name) {
    return "hello" + name;
}

}

`

效果:

这里写图片描述

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值