资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何使用SpringMVC与springfox-swagger2搭建一个restfulAPI

如何使用Spring MVC与springfox-swagger2搭建一个restful API?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

古塔网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联公司于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司

前言

在集成springfox-swagger2之前,我也尝试着集成了swagger-springmvc,方式差不多,但是swagger-springmvc相对麻烦一点,因为要把它的静态文件copy到自己的项目中。

方法如下

这里先写下需要的pom.xml配置(我引用的2.4.0,相对稳定)

 
 io.springfox 
 springfox-swagger2 
 2.4.0 
  
  
 io.springfox 
 springfox-swagger-ui 
 2.4.0 
  

还需要在spring-mvc.xml中添加映射静态的配置:

然后就是swagger2的配置类:

package com.xingguo.logistics.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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
@EnableSwagger2
public class SwaggerConfig {

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

 private ApiInfo buildApiInf(){
 return new ApiInfoBuilder()
  .title("xingguo大标题")
  .termsOfServiceUrl("http://blog.csdn.net/u014231523网址链接")
  .description("springmvc swagger2")
  .contact(new Contact("diaoxingguo", "http://blog.csdn.net/u014231523", "diaoxingguo@163.com"))
  .build();

 }
}

然后运行项目,输入自己的url。

http://{ip}:{port}/{projectname}/swagger-ui.html#/

我的url:

http://localhost:8989/logistics/swagger-ui.html#/

然后就可以看到效果图:

如何使用Spring MVC与springfox-swagger2搭建一个restful API
如何使用Spring MVC与springfox-swagger2搭建一个restful API

它会把按照controller,把所有的接口都加载进来。

我的目录结构如图:

如何使用Spring MVC与springfox-swagger2搭建一个restful API

然后,就是接口名称和参数的说明:

常用注解:

      - @Api()用于类名

      - @ApiOperation()用于方法名

      - @ApiParam()用于参数说明

      - @ApiModel()用于实体类

      - @ApiModelProperty用于实体类属性

更详细的说明请参见官方注解说明文档

使用方法如图:

@Controller
//类上使用@Api
@Api(value="用户controller",description="用户相关操作")
public class UserController {

 @RequestMapping(value="index",method=RequestMethod.POST)
 //方法上使用@ApiOperation
 @ApiOperation(value="首页",notes="跳转到首页")
 //参数使用@ApiParam
 public Object getIndex(@ApiParam(name="topic实体",value="json格式",required=true) @RequestBody Topic topic){
 //业务内容,被我删除了,请忽略,主要看上面的注解
 Object obj = new Object();
 return obj;
 }
}
//一般添加个@ApiModel()就可以,看情况使用里面的属性
@ApiModel(value="Topic", discriminator = "foo", subTypes = {Topic.class})
public class Topic{

}

效果图如下:

如何使用Spring MVC与springfox-swagger2搭建一个restful API

我在springboot中也集成了swagger2,集成方式基本相同,使用方式也基本一样。请参考Spring Boot集成springfox-swagger2构建restful API的方法教程

关于如何使用Spring MVC与springfox-swagger2搭建一个restful API问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享题目:如何使用SpringMVC与springfox-swagger2搭建一个restfulAPI
URL标题:http://cdkjz.cn/article/ppipeo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220