版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/55047193

1、Swagger2是什么?

Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。

2、Swagger2官网

官网地址

3.Swagger2的入门教程

3.1Swagger2的maven依赖

<!-- 构建Restful API --><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>

3.2RestApiConfig的配置

package com.lidong.dubbo.web.util;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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** @项目名称:lidong-dubbo* @类名:RestApiConfig* @类的描述: Restapi的基本配置* @作者:lidong* @创建时间:2017/2/11 上午10:01* @公司:chni* @QQ:1561281670* @邮箱:lidong1665@163.com* @使用方法:Restful API 访问路径: http://localhost:8080/lidong-dubbo-web/swagger-ui.html*/
@EnableWebMvc
@EnableSwagger2
@Configuration
@ComponentScan(basePackages ="com.lidong.dubbo")
public class RestApiConfig extends WebMvcConfigurationSupport {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.lidong.dubbo.web")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SpringMVC中使用Swagger2构建RESTful APIs").termsOfServiceUrl("https://github.com/lidong1665").contact(new Contact("请叫我小东子","http://blog.csdn.net/u010046908","lidong1665@163.com")).version("1.0.0").build();}}

3.3在Controller中添加

@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")@RequestMapping(value = "getUserForid/{id}", method = RequestMethod.GET)@ResponseBodypublic String getUser(@PathVariable int id) {try {return JsonUtil.bean2json(userService.getUserById(id));} catch (Exception e) {e.printStackTrace();}return null;}

3.4结果

代码地址

dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3(十)之Spring MVC中使用 Swagger2 构建Restful API...相关推荐

  1. Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

    [0]README 0)本文旨在 review Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合过程: 1)项目整合所涉及的源代码,please visit  ht ...

  2. Spring4.1新特性——Spring MVC增强

    2019独角兽企业重金招聘Python工程师标准>>> 1.GroovyWebApplicationContext  在Spring 4.1之前没有提供Web集成的Applicati ...

  3. 搭建Spring4+Spring MVC web工程的最佳实践

     Spring是个非常非常非常优秀的java框架,主要是用它的IOC容器帮我们依赖注入和管理一些程序中的Bean组件,实现低耦合关联,最终提高系统可扩展性和可维护性,用它来辅助我们构建web工程将 ...

  4. springmvc4 ajax 406,Spring4 MVC 中,jQuery ajax (406 Not Acceptable)

    最近使用spring4.0的Mvc,json请求时,客户端报错,406 Not Acceptable 1.导入第三方的jackson包,在pom.xml中添加: com.fasterxml.jacks ...

  5. SSM(Spring4.x.x+SpringMVC4.x.x+Mybatis3.4.x)框架整合

    本文是参考SSM框架--详细整合教程(Spring+SpringMVC+MyBatis)修改而来的 一.环境 1. Myeclipse2016 2. Mysql 二.具体步骤 1. 整合Spring和 ...

  6. 使用IDEA整合spring4+spring mvc+hibernate

    配置文件  spring-mvc.xml 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans ...

  7. spring4.x aop拦截spring mvc controller

    1  在spring-mvc.xml中,配置信息 <!-- 启动对@AspectJ注解的支持 --> <aop:aspectj-autoproxy/> <aop:aspe ...

  8. Bookmarks(三)

    Bookmarks 书签栏 tooltips提示效果,支持点击与经过显示,位置和效果可以自定义 - CSDN博客 疯狂的小萝卜头 - 博客园 [Kettle从零开始]第九弹之Kettle定时任务介绍 ...

  9. 【笔记】spring的注解回顾,springboot-restful项目结构介绍 springboot-freemarker ⼯程配置详解

    注解 学Spring boot有一阵子了,总结一下它的注解. @Controller :修饰class,⽤来创建处理http请求的对象 @RestController :Spring4之后加⼊的注解, ...

最新文章

  1. 腾讯 AI Lab 开源业内最大规模多标签图像数据集
  2. R语言ggplot2可视化自定义图例实战:添加自定义的图例、添加填充色的图例
  3. nginx 缓存时间说明
  4. 删除fedora多余内核:解决每次升级后旧内核还会存在的问题
  5. .NET程序崩溃了怎么抓 Dump ? 我总结了三种方案
  6. Java核心类库篇4——集合
  7. IntelliJ IDEA中怎么创建xml文件?
  8. 55.函数模板指针匹配(模板自动匹配*多的)
  9. Flink 实战:如何解决生产环境中的技术难题?
  10. Thinkphp双轨直销系统源码
  11. 五大常用算法:贪心算法
  12. 【软考 系统架构设计师】软件架构设计④ 基于架构的软件开发方法
  13. 计算机网络管理师2级,计算机网络管理员(二级)操作技能考核试卷
  14. speedoffice如何在Word表格中插入行或列
  15. 个人微信小程序开发入门教程:注册个人小程序
  16. wazuh-monitord agent连接监控
  17. 安卓虚拟键盘_安卓这些年变化多惊人?老玩家的回忆杀
  18. Boost PFC参数计算——PFC电感
  19. 2021-11-25 工作记录--LayUI-让select下拉菜单的已选值清空
  20. C++ functional

热门文章

  1. git本地仓库关联远端仓库
  2. FTP匿名登录或弱口令漏洞及服务加固
  3. rock-paper-scissors
  4. 阿里mysql同步工具otter的docker镜像
  5. mysql存储过程写法—动态参数运用
  6. 设置Flex toolTip的样式
  7. operate XML file (Open,Insert)
  8. CNN笔记:通俗理解卷积神经网络
  9. 大数据驱动下的微博社会化推荐
  10. 数据库以及后台开发之写在前面