前提:maven,ssm,不是springboot项目

1.在maven中添加依赖

 <!-- Swagger2 Begin -->
<!--springfox的核心jar包-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<!--springfox-ui的jar包(里面包含了swagger的界面静态文件)-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<!--springfox依赖的jar包;如果你的项目中已经集成了无需重复-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- Swagger2 End -->

2.创建专门的swagger包用于存放swagger的配置文件,非必须,条理清晰罢了

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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; /*重要!如果你的项目引入junit测试,此处需要使用@WebAppConfiguration,如果没有使用junit使用@Configuration(很多的博客都没有注明这个问题,为此我花了非常多的时间解决问题)*/
@WebAppConfiguration
@EnableSwagger2//重要!
@EnableWebMvc
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("cn.yonyong.usetk.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("iToken API 文档")
.description("iToken API 网关接口,http://www.funtl.com")
.termsOfServiceUrl("http://www.faramita.online")
.version("1.0.0")
.build();
}
}

3.在springMVC的配置文件中添加swagger的配置

<!--将静态资源交由默认的servlet处理-->
<mvc:default-servlet-handler />
<!--向容器自动注入配置-->
<context:annotation-config />
<!-- 自动扫描 @Controller 与 swagger.java -->
<context:component-scan base-package="cn.yonyong.*.controller,cn.yonyong.usetk.config.swagger"/>
<!--重要!配置swagger资源不被拦截-->
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

4.修改web.xml文件中配置所有的请求都经DispatcherServlet处理

<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
  1. controller的配置
@Controller
@RequestMapping("/user")
@Api(value = "/user", tags = "User接口")
public class UserController { private static Logger logger = Logger.getLogger(UserController.class);
@Autowired
private UserService userService; @RequestMapping(value = "/getUser/{id}",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息", httpMethod = "GET", response = User.class)
public ResponseEntity<User> getUser(@PathVariable int id){
User user = userService.getUserById(id);
logger.info("controller:"+user);
return ResponseEntity.ok(user);
}
}
  1. 项目访问地址
//访问地址:[项目访问地址]/swagger-ui.html
//例如本地Tomcat运行swagger-demo,那么访问地址为:http://localhost:8080/swagger-demo/swagger-ui.html

7.api注解的详细讲解

https://segmentfault.com/a/1190000010465989

SSM非springboot配置swagger2的更多相关文章

  1. JAVA入门[23]-SpringBoot配置Swagger2

    一.新建SpringBoot站点 1.新建module,然后引入pom依赖: <parent> <groupId>org.springframework.boot</gr ...

  2. SpringBoot配置swagger2(亲测有效,如果没有配置成功,欢迎在下方留言)

    一.导包: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  3. springboot 配置 swagger2

    1.pom.xml 添加依赖 <!--swagger2 依赖--> <dependency> <groupId>io.springfox</groupId&g ...

  4. springboot配置swagger2

    .在pom.xml里添加jar包: <dependency> <groupId>io.springfox</groupId> <artifactId>s ...

  5. IDEA springboot配置

    基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...

  6. springboot新增swagger2配置

    转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...

  7. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  8. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  9. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

随机推荐

  1. 自制反汇编工具使用实例 其二(使用xmm寄存器初始化对象,以及空的成员函数指针)

    在反汇编代码中,当看到xmm寄存器,第一反应是将要进行浮点操作或访问,但是更加多的情况是在使用xmm寄存器初始化局部对象. 下面是自制反汇编工具翻译出来的代码: // -[CALayer setAll ...

  2. linux运维与实践

    1.容器云计算节点负载值高,通过top可以看到Load Average:70.1  71.3  70.8,虚拟机有8个cpu: cpu使用率高导致(R状态)? 同时在top中观察一段时间,消耗cpu最 ...

  3. Linux 命令记录

    记录Linux下使用过的命令: Linux端 1.测试当前系统支持语言(我这用的是xshell,如果出现乱码,则在file-properties-terminal-encoding中,设置为utf-8 ...

  4. Centos7 搭建LAMP环境(编译安装)

    1.查看系统版本 [niemx@localhost ~]$ cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 2.安装软件准备 ...

  5. (二)OpenStack---M版---双节点搭建---数据库安装和配置

    ↓↓↓↓↓↓↓↓视频已上线B站↓↓↓↓↓↓↓↓ >>>>>>传送门 本章节只在Controller节点执行 1.安装Mariadb数据库来存储信息 2.NoSQL数 ...

  6. 【2018寒假集训 Day2】【动态规划】钱币兑换(exchange)(自己翻译的2333)

    钱币兑换(exchange) 问题描述: Dave偶然获得了未来几天的美元(dollars)与马克(marks)之间的兑换率.例如Dave开始有100marks,请编写个程序帮助Dave找出最好的买卖 ...

  7. Fortran数组、函数--xdd

    1.数组的声明 integer,parameter::num= integer::student(num) 或者 integer a() 或者 integer a(,) student()= !第一个 ...

  8. saltstack 自动化运维

    salt介绍 saltstack是由thomas Hatch于2011年创建的一个开源项目,设计初衷是为了实现一个快速的远程执行系统. salt强大吗 系统管理员日常会进行大量的重复性操作,例如安装软 ...

  9. Kotlin协程通信机制: Channel

    Coroutines Channels Java中的多线程通信, 总会涉及到共享状态(shared mutable state)的读写, 有同步, 死锁等问题要处理. 协程中的Channel用于协程间 ...

  10. Python内置类属性,元类研究

    Python内置类属性 我觉得一切都是对象,对象和元类对象,类对象其实都是一样的,我在最后进行了证明,但是只能证明一半,最后由于元类的父类是type,他可以阻挡对object属性的访问,告终 __di ...