0
点赞
收藏
分享

微信扫一扫

Swagger与SpringBoot的整合以及Swagger2简单配置即可


Swagger可视化API,不仅能查看API,还能测试

1.引入相关JAR

 

<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>


2.在启动类中加入Swagger相关代码(用的springboot):


@EnableSwagger public class AppServiceApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(AppServiceApplication.class); Set<Object> sourcesSet = new HashSet<Object>(); sourcesSet.add("classpath:applicationContext.xml"); application.setSources(sourcesSet); application.run(args); } private SpringSwaggerConfig springSwaggerConfig; /** * Required to autowire SpringSwaggerConfig */ @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; } /** * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc * framework - allowing for multiple swagger groups i.e. same code base * multiple swagger resource listings. */ @Bean public SwaggerSpringMvcPlugin customImplementation() { return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?"); } private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo( "App Service API", "", "", "huqc@gcks.cn", "", ""); return apiInfo; }


3.在代码中添加相关APIAnnotation,如下:

<span style="font-size:18px;background-color: rgb(255, 255, 255);">@ResponseBody
@RequestMapping(
value = addUser, method = RequestMethod.POST, produces = application/json; charset=utf-8)
@ApiOperation(value = 添加用户, httpMethod = POST, response = BaseResultVo.class, notes = add user)
public String addUser(@ApiParam(required = true, name = postData, value = 用户信息json数据) @RequestParam(
value = postData) String postData, HttpServletRequest request)
{
LOGGER.debug(String.format(at function, %s, postData));
if (null == postData || postData.isEmpty())
{
return super.buildFailedResultInfo(-1, post data is empty!);
}

UserInfo user = JSON.parseObject(postData, UserInfo.class);
int result = userService.addUser(user);
return buildSuccessResultInfo(result);
}</span>

说明:
其中@ApiOperation和@ApiParam为添加的API相关注解,个参数说明如下:
@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”;其他参数可参考源码;
@ApiParam(required = “是否必须参数”, name = “参数名称”, value = “参数具体描述”

 

4.添加Swagger UI配置

在GitHub上​​下载​​SwaggerUI项目,将dist下所有内容拷贝到本地项目webapp下面,结果目录所示:

修改index.html

将index.html中http://petstore.swagger.wordnik.com/v2/swagger.json修改为http://localhost:8080/{projectname}/api-docs

到此为止,所有配置完成,启动你的项目,访问http://localhost:8086/swagger/index.html即可看到如下所示页面:

但是swagger不建议像上面和代码混合在一块,建议swagger用yaml文件单独配置部署生成HTML文件

 

实际上自从swagger2出现后,一切就变得简单了,只需要三步:

1、引入Swagger2 的依赖:

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

 2、配置Swagger.java 类

@Configuration
@EnableSwagger2
public class Swagger {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.breakli.app.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("技术人的突破平台接口")
.description("Swagger平台测试")
.termsOfServiceUr
.version("1.0")
.build();
}
}

3、在具体接口类名上配置:@Api(tags={"公共字典接口"})

方法配置:@ApiOperation(value = "根据字典类型查询字典列表接口")

例子如下:

@Slf4j
@Controller
@RequestMapping(value = "/api/dict")
@Api(tags={"公共字典接口"})
public class DictController {

@ResponseBody
@GetMapping(value = "/list")
@ApiOperation(value = "根据字典类型查询字典列表接口")
public Result findAll (@RequestParam(required = true) int dictType) {

}
}

 

 

 

 

 

 

 

 

 

 

举报

相关推荐

0 条评论