idea maven + Springmvc 整合 Swagger 总结

栏目: 后端 · 发布时间: 5年前

内容简介:在日常接口开发过程中,为了方便前端同事使用我们开发的接口,通过我们需要编写各种形式的接口文档。有Word版的 有apiDoc 版的,这次我来简单概述一下在我们已有的springmvc 项目中如何使用swagger工具首先。我默认你本地已经存在有正常可运行的springmvc项目 且 maven 构建的

在日常接口开发过程中,为了方便前端同事使用我们开发的接口,通过我们需要编写各种形式的接口文档。

有Word版的 有apiDoc 版的,这次我来简单概述一下在我们已有的springmvc 项目中如何使用swagger工具

首先。我默认你本地已经存在有正常可运行的springmvc项目 且 maven 构建的

那么我们现在可以继续啦。

1.修改 pom.xm 文件 新增以下依赖

<!-- swagger2 核心依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!-- swagger-ui 为项目提供api展示及测试的界面 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!-- 集成 swagger 的时候,缺少这个 jar包是不OK的-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.6</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.6</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>

2. 然后工程里新增一个包 或者在你现有的包中新增一个class ,本人是新增了一个swagger.utils包 然后再创建ApiConfig.class

代码如下:

package swargger.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
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
@EnableSwagger2
@EnableWebMvc
public class ApiConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("对外开放接口API 文档")
                .description("HTTP对外开放接口")
                .version("1.0.0")
                .termsOfServiceUrl("http://xxx.xxx.com")
                .license("LICENSE")
                .licenseUrl("http://xxx.xxx.com")
                .build();
    }

}

3. 修改resources目录下的spring的xml配置文件,有的同学喜欢命名为 application.xml  有的喜欢名为spring-mvc.xml spring-servlet.xml  你们 跟据自身实际情况吧。

新增配置如下

<mvc:default-servlet-handler />
    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />
 <context:component-scan base-package="swargger.*"/>

完工。我们写个测试controller来试试

package ifaster.controller.home;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping("student")
public class StudentController {



    //@ResponseBody//(之前我因为加了这个注解,导致页面访问一直是406错误,注释了就好啦,具体为啥我暂时还不知道)
    @ApiOperation(value = "获得所有的学生对象list", notes = "get请求,查询所有的学生。")
    @RequestMapping(value = "/getAllStudent", method = RequestMethod.GET)
    public ModelAndView getAllStudent() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("studentDisplay");
        mav.addObject("students", 123);
        return mav;
    }

    @ApiOperation(value = "根据学生的name,获得单个学生的信息", notes = "根据学生的name,查询学生对象的信息。")
    @ApiImplicitParam(name = "name", value = "学生的名称", required = true, dataType = "String")
    @ResponseBody
    @RequestMapping(value = "getStudentByName", method = RequestMethod.POST)
    public String getStudentByName(String name) {
        return "";
    }

    @ApiOperation(value = "根据学生的name和age,获得单个学生的信息", notes = "根据学生的name和age,查询学生对象的信息。")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "学生名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "age", value = "学生年龄", required = true, dataType = "int")})
    @ResponseBody
    @RequestMapping(value = "getStudentByNameAndAge", method = RequestMethod.POST)
    public String getStudentByName(String name, int age) {
        return "";
    }

    @ApiOperation(value = "新建学生对象到数据库", notes = "新建数据到数据库。")
    @ApiImplicitParam(name = "student", value = "学生对象", required = true, dataType = "Student")
    @ResponseBody
    @RequestMapping(value = "createNewStudent", method = RequestMethod.POST)
    public String create(@RequestBody String student) {
        return "";
    }
}

最后访问地址

http://localhost:8080/swagger-ui.html#/

大功告成!

idea maven + Springmvc 整合 Swagger 总结


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Erlang趣学指南

Erlang趣学指南

邓辉、孙鸣 / 人民邮电出版社 / 2016-9-7 / 79.00元

这是一本讲解Erlang编程语言的入门指南,内容通俗易懂,插图生动幽默,示例短小清晰,结构安排合理。书中从Erlang的基础知识讲起,融汇所有的基本概念和语法。内容涉及模块、函数、类型、递归、错误和异常、常用数据结构、并行编程、多处理、OTP、事件处理,以及所有Erlang的重要特性和强大功能。一起来看看 《Erlang趣学指南》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具