首先呢我们导入相关的jar包文件

为了方便copy我copy一份

<!-- 导入java ee jar 包 -->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <!-- spring核心 jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <!-- jacksion -->
    
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${swagger.version}</version>
        </dependency>

以上jar包有 spring的ioc的bean 上下文的context 还有springmvc的web 以集webmc  swagger2的整合還有swagger2返回的json数据用到的jar包

接下来我们分层编写

我嫩以User用户为案例 编写实体类

package cn.bdqn.beans;

/**
 * 实体类
 * @author ljn
 * @date 2017-10-21
 * @vesion 1.0
 * @detail
 */
public class User {

protected Integer id;
    protected String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public User(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public User() {
        
    }
    
    
}

接下来我们编写  SwaggerConfig类文件

copy 如下:

package cn.bdqn.swagger;

import io.swagger.annotations.BasicAuthDefinition;
import io.swagger.annotations.SwaggerDefinition;

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 springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 启动@EnableSwagger2 swagger2 @EnableWebMvc mvc项目  扫描我的controller类  @componentScan
 * 配置变量
 * @author ljn
 * @date 2017-10-21
 * @vesion 1.0
 * @detail
 */
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages={"cn.bdqn.controller"})
public class SwaggerConfig {

@Bean
    public Docket getDocket(){
        
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(getApiInfo());
        
    }
    public ApiInfo getApiInfo(){
        ApiInfoBuilder ab=new ApiInfoBuilder();
        ab.title("title");
        ab.description("描述");
        return    ab.build();
    }
    
    
}

最后我们编写controller

以下是代码
@Controller("userController")
@Api(value = "干嘛的类", description = "描述")
@RequestMapping("user")
public class UserController {

@RequestMapping("getLists")
    @ApiOperation(value = "查询集合", response = User.class, httpMethod = "POST")
    public List<User> getLists(@ApiParam(value = "title") String title) {
        List<User> lists = new ArrayList<User>();
        User user = new User();
        user.setName(title);
        user.setId(1);
        User user1 = new User();
        user1.setName(title);
        user1.setId(1);
        lists.add(user);
        lists.add(user1);
        return lists;

}

}
还有编写 springmvc.xml 文件

copy 过来 如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:ctx="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 开启注解 -->
    <mvc:annotation-driven />
    <!-- 扫描controller -->
    <ctx:component-scan base-package="cn.bdqn.controller.UserController" />
    <!-- 生成bean的swagger文件 -->
    <bean class="cn.bdqn.swagger.SwaggerConfig"></bean>
    <!-- 视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/META-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- swagger的静态文件放行 -->
    <mvc:resources location="classpath:/META-INF/resources/"
        mapping="swagger-ui.html" />
    <mvc:resources location="classpath:/META-INF/resources/webjars/"
        mapping="/webjars/**" />
</beans>
           
            最后编写 web.xml文件

然后我们启动服务器tomcat 访问

localhost:8080/bokeyuan/swagger-ui.html  这是swagger api的路径

springmvc与swagger2的更多相关文章

  1. 快速构建springmvc+spring+swagger2环境

    快速构建springmvc+spring+swagger2环境 开发工具:Intellij idea               jdk: 1.8 开发步骤: 1.创建maven工程,如图建立工程结构 ...

  2. 利用Swagger2自动生成对外接口的文档

    一直以来做对外的接口文档都比较原始,基本上都是手写的文档传来传去,最近发现了一个新玩具,可以在接口上省去不少麻烦. swagger是一款方便展示的API文档框架.它可以将接口的类型最全面的展示给对方开 ...

  3. SpringMVC+JWT+Swagger UI+RestFul

    前言: 其实很早就想写这篇文章了,因为我觉得这会对很多新手有指引作用,当初自己也是瞎子过河的摸索着过来的.目前后台开发比较流行的MVC框架中使用Spring MVC还是比较多的,当然还有Spring ...

  4. SpringMVC中使用Swagger2整合

    Swagger2是什么 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 W ...

  5. MP实战系列(十)之SpringMVC集成SpringFox+Swagger2

    该示例基于之前的实战系列,如果公司框架是使用JDK7以上及其Spring+MyBatis+SpringMVC/Spring+MyBatis Plus+SpringMVC可直接参考该实例. 不过建议最好 ...

  6. springmvc+swagger2

    一.swagger2依赖 <!--swagger--> <dependency> <groupId>io.springfox</groupId> < ...

  7. 03、Swagger2和Springmvc整合详细记录(爬坑记录)

    时间 内容 备注 2018年6月18日 基本使用 spirngmvc整合swagger2 开始之前这个系列博文基本是,在项目的使用中一些模块的内容记录,但是后期逐渐优化,不单单是整合内容. swagg ...

  8. 使用Swagger2构建SpringMVC项目中的Restful API文档

    使用Swagger自动生成API文档,不仅增加了项目的可维护性,还提高了API的透明度更利于快速测试等工作,便于更快地发现和解决问题. 本篇文章只记录整合过程,关于Security Configura ...

  9. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

随机推荐

  1. AOP 总结

    AOP即Aspect oriented Programing, 面向切面编程. 相关术语: 通知(Advice): Advice defineds when to execute what actio ...

  2. python-django项目-每次重启电脑需要启动的虚拟机服务_20191124

    python-django项目-每次重启电脑需要启动的虚拟机服务 看来第一步是确定虚拟机的ip问题,必须要是192.168.100.128,否则很多的配置都不能用了, 所以要配置虚拟机的ip, 第一步 ...

  3. 【Java集合】试读LinkedList源码

    LinkedList的本质是双向链表.(01) LinkedList继承于AbstractSequentialList,并且实现了Dequeue接口. (02) LinkedList包含两个重要的成员 ...

  4. 1 jquery对checkbox的简单操作

    //全选和全不选 votefunction selectAll(){ if($(":checkbox").prop('checked')){     //$(":chec ...

  5. undefined reference to 问题汇总及解决方法 ----- 还有一种问题没有解决(可能是顺序问题)

    1.链接时缺失了相关的目标文件 2.链接时缺少了相关的库文件 3.链接的库文件中有使用了另一个库文件 4.多个库文件链接顺序问题 5.定义与实现不一致 6.在c++代码中链接C语言的库   转载地址: ...

  6. vue-cli 项目结构介绍

    感谢:https://www.jianshu.com/p/7006a663fb9f 总体框架 一个vue-cli的项目结构如下,其中src文件夹是需要掌握的,所以本文也重点讲解其中的文件,至于其他相关 ...

  7. 吴裕雄--python学习笔记:os模块的使用

    在自动化测试中,经常需要查找操作文件,比如说查找配置文件(从而读取配置文件的信息),查找测试报告(从而发送测试报告邮件),经常要对大量文件和大量路径进行操作,这就依赖于os模块. 1.当前路径及路径下 ...

  8. 如何设计一个LRU Cache

    如何设计一个LRU Cache? Google和百度的面试题都出现了设计一个Cache的题目,什么是Cache,如何设计简单的Cache,通过搜集资料,本文给出个总结. 通常的问题描述可以是这样: Q ...

  9. 机器学习入门教程-k-近邻

    k-近邻算法原理 像之前提到的那样,机器学习的一个要点就是分类,对于分类来说有许多不同的算法,所谓的物以聚类,分以群分.我们非常的清楚,一个地域的人群,不管在生活习惯,还是在习俗上都是非常相似的,也就 ...

  10. Python---3基础输入方法

    一字符串写法 1.单一字符串 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hell ...