您的位置:首页 > 其它

jeesite中集成Swagger2 三步

2017-11-22 09:44 218 查看

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

}


`

效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: