在spring+springMvc+mabatis框架下集成swagger
我是在ssm框架下集成swagger的,具体的ssm搭建可以看这篇博文:
Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构
本项目的GitHub地址:https://github.com/chenyangsocool/ssm.git
接下去就正式开始了:
1.通过maven导入相关swagger的jar包:
<!-- swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.</version>
</dependency>
<!-- /swagger -->
2.在com.chenyangsocool.ssm.tools下新建swagger/Swagger2Config.java:
package com.chenyangsocool.ssm.tools.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import io.swagger.annotations.ApiOperation;
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; @Configuration //让Spring来加载该类配置
@EnableWebMvc //启用Mvc,非springboot框架需要引入注解@EnableWebMvc
@EnableSwagger2 //启用Swagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()).select()
//扫描指定包中的swagger注解
//.apis(RequestHandlerSelectors.basePackage("com.xia.controller"))
//扫描所有有注解的api,用这种方式更灵活
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SSM RESTful APIs")
.description("This API Document is based on RESTful Style, The description is detail and auto-generation, It's very friendly for developers.")
.termsOfServiceUrl("http://www.cnblogs.com/chenyangsocool/")
.contact("Young")
.version("1.0.0")
.build();
}
}
3.在实体类中创建注释,以Test实体类为例:
package com.chenyangsocool.ssm.model; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; @ApiModel(value="Test",description="Test")//对类进行swagger注解
public class Test {
@ApiModelProperty(value="测试id",name="id")//对类的字段属性进行swagger注解
private int id; @ApiModelProperty(value="测试内容",name="context")
private String context; @ApiModelProperty(value="测试内容的浏览数",name="viewCount")
private int viewCount; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getContext() {
return context;
} public void setContext(String context) {
this.context = context;
} public int getViewCount() {
return viewCount;
} public void setViewCount(int viewCount) {
this.viewCount = viewCount;
} @Override
public String toString() {
return "Test{" +
"id=" + id +
", context='" + context + '\'' +
", viewCount=" + viewCount +
'}';
}
}
4.在TestController中添加swagger相关代码:
package com.chenyangsocool.ssm.controller; import com.chenyangsocool.ssm.model.Test;
import com.chenyangsocool.ssm.service.ITestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; @Controller
@RequestMapping("/test")
@Api(value = "测试信息", tags = {"测试相关接口"})//swagger控制器说明注解
public class TestController { @Resource
private ITestService testService; @RequestMapping("/index_page")
public String showIndex(HttpServletRequest request, Model model) {
int id = Integer.parseInt(request.getParameter("id"));
Test test = this.testService.getModelById(id);
//绑定对象到test/index.jsp
model.addAttribute("test", test);
return "test/index";
} @RequestMapping("/index_api")
@ResponseBody
@ApiOperation(value = "获取单个测试实例", notes = "传入一个id,获取该id对应的实例。",httpMethod = "GET")//swagger方法注解
public Test Index(HttpServletRequest request,Model model) {
int id = Integer.parseInt(request.getParameter("id"));
return this.testService.getModelById(id);
}
}
5.访问:
http://localhost:8080/ssm/swagger-ui.html
即可查看所定义的api接口列表:
注:
更多注解可以网上自己搜索
本项目的GitHub地址:https://github.com/chenyangsocool/ssm.git
后续思考:
实体类是由mybatis自动生成,那实体类中的swagger注解是否也可以由mybatis自动生成?
等我有空就把文章写上来~~
参考文章:
SpringMVC集成Swagger插件以及Swagger注解的简单使用
在spring+springMvc+mabatis框架下集成swagger的更多相关文章
- Spring+SpringMvc+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- SSM(Spring + Springmvc + Mybatis)框架面试题
JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建四:配置springmvc
在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试的基础上 继续进行springmvc的配置 一:配置完善web.xml文件
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 构建目录环境和依赖)
引言:在用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一 的基础上 继续进行项目搭建 该部分的主要目的是测通MyBatis 及Spring-dao ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建一:建立MAVEN Web项目
一:创建maven web项目er
- SSH(Spring SpringMVC Hibernate)框架整合
项目说明: 使用SSH(Spring SpringMVC Hibernate)框架整合添加部门功能 项目结构 1.导入依赖jar包 <!--单测--> <dependency&g ...
- SSM(Spring +SpringMVC + Mybatis)框架搭建
SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...
- SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...
- SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)
原文:https://blog.csdn.net/yijiemamin/article/details/51156189# 这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文 ...
随机推荐
- Kafka 0.7.2 单机环境搭建
Kafka 0.7.2 单机环境搭建当下载完Kafka后,进行解压,其目录结构如下: bin config contrib core DISCLAIMER examples lib lib_manag ...
- MVC 3.0错误 HTTP 404您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。
MVC3.0框架开发项目: 有时在程序运行的时候会出现“HTTP 404.您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用.请检查以下 URL 并确保其拼写正确.”的错 ...
- 三分钟教你学Git (四)之紧急救助
假设你不小心git reset --hard HEAD^ 然后这个commit又没有在别的git仓库中,怎么办?是不是这次改动就丢了呢? 当然不是,git为我们每次都历史都保留了reference l ...
- 剑客vs刀客 Java vs .NET
刀,无鞘的刀,重三十六斤六两三分,刀重而不大,重而不笨,千年寒铁精炼而成,刀身颀长,刀背轻薄,锋利异常,刀身桔黄色,隐隐泛着青色,刀面嵌龙凤图案,似龙吟,似凤鸣.刀柄带有两环,轻轻撞击会发出" ...
- .Net线程问题解答
基础篇 怎样创建一个线程 受托管的线程与 Windows线程 前台线程与后台线程 名为BeginXXX和EndXXX的方法是做什么用的 异步和多线程有什么关联 WinForm多线程编程篇 我的多线程W ...
- java生成自己定义的表ID
需生成例如以下ID: 56d7ade1-87d1-4f54-8dc8-13611c8c2545 27181ad4-4214-4e12-af3a-911a0103a12f 24cafdfb-eac3-4 ...
- linux查找超过一定时间的文件,并批量删除
1.find . -maxdepth 4 -name "*-*" -mtime 3 -maxdepth的值决定是否对下面的子目录进行递归查找 -mtime 3表示查找刚好3天的: ...
- 写的一个split函数
vector<string> strsplit(const string& str) { vector<string> vec; string sstr1=str, s ...
- C# ?(问号)的三个用处
public DateTime? StatusDateTime = null; 脑子便也出现个问号,这是什么意思呢?网上搜下,总结如下: 1. 可空类型修饰符(?): 引用类型可以使用空引用表示一个不 ...
- OpenGL进阶(十四) - UVN Camera实现
提要 3D游戏中最基本的一个功能就是3D漫游了,玩家可以通过键盘或者鼠标控制自己的视角. 之前我们也学习过一个相关的函数,glLookAt,用来制定摄像机的位置,摄像机观察目标位置,还有摄像机的放置方 ...