美文网首页
SpringBoot整合Swagger

SpringBoot整合Swagger

作者: 左羊 | 来源:发表于2019-07-23 23:16 被阅读0次
/**
 * 环境 
 * Windows 7 
 * SpringBoot 2.0.2 
 * Swagger 2.8.0 
 * Eclipse Photo
*/
image

什么是swagger?

Swagger的目标是为REST APIs 定义一个标准的,与语言无关的接口,使人和计算机在看不到源码或者看不到文档或者不能通过网络流量检测的情况下能发现和理解各种服务的功能。当服务通过Swagger定义,消费者就能与远程的服务互动通过少量的实现逻辑。类似于低级编程接口,Swagger去掉了调用服务时的很多猜测。 浏览 Swagger-Spec 去了解更多关于Swagger 项目的信息,包括附加的支持其他语言的库。

准备pom文件

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.dxf</groupId>
        <artifactId>myspringboot</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>myspringboot-swagger</artifactId>
    <name>myspringboot-swagger</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

配置文件yml

server:
  port: 80

首先配置Swagger(Swagger2Configjava)

package com.dxf.myspringboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket getDocket() { // 此类主要是整个的Swagger配置项,利用这个类需要来指派扫描包
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.getApiInfo()).select()
                .apis(RequestHandlerSelectors
                        .basePackage("com.dxf.myspringboot.controller"))
                .paths(PathSelectors.any()).build(); // 设置文档的显示类型
    }
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder().title("SpringBoot中使用Swagger构建项目说明信息")
                .description("更多选择请关注www.wukonglai.com")
                .termsOfServiceUrl("http://www.wukonglai.com").contact("悟空来-小白龙")
                .license("独善其身的狗").version("1.0").build();
    }
}

model类编写(User.java)

package com.dxf.myspringboot.model;

import java.util.Date;

public class User {
private int uid;
private String name;
private Date birthday;
private double salary;
private int age;
public int getUid() {
    return uid;
}
public void setUid(int uid) {
    this.uid = uid;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Date getBirthday() {
    return birthday;
}
public void setBirthday(Date birthday) {
    this.birthday = birthday;
}
public double getSalary() {
    return salary;
}
public void setSalary(double salary) {
    this.salary = salary;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
@Override
public String toString() {
    return "User [uid=" + uid + ", name=" + name + ", birthday=" + birthday + ", salary=" + salary + ", age=" + age
            + "]";
}
}

Controller类编写(UserController.java)

package com.dxf.myspringboot.controller;

import java.util.Date;

import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import com.dxf.myspringboot.model.User;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
public class UserController {
    @ApiOperation(value = "实现人员信息的添加处理", notes = "就是加人的,多么的简单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户描述的详细实体信息", required = true, dataType = "User")})
    @RequestMapping(value = "/user/add", method = RequestMethod.POST)
    public Object add(@RequestBody User user) { // 表示当前的配置可以直接将参数变为VO对象
        System.err.println("【UserController.add()接收对象】" + user);
        return true;
    }
    @ApiOperation(value = "获取指定编号的人员信息", notes = "只需要设置uid的信息就可以获取User的完整内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户编号", required = true, dataType = "String")})
    @RequestMapping(value = "/user/get/{uid}", method = RequestMethod.GET)
    public User get(@PathVariable("uid") int uid) {
        User user = new User();
        user.setUid(uid);
        user.setAge(12);
        user.setBirthday(new Date());
        user.setSalary(2000.00);
        user.setName("Tom");
        System.err.println("【UserController.get()接收对象】" + user);
        return user;
        
    }
}

SpringBoot启动类(StartSpringBootMain.java)

package com.dxf.myspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching 
@SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartSpringBootMain.class, args);
    }
}

测试

正常进行程序的启动配置处理,而后打开浏览器进入到界面:http://localhost/swagger-ui.html

image
接下来我们在其中进行一些操作
【user-controller】-->【/user/add】
image
【try is out】
image
image
之后看下后台信息是否输出
image

相关文章

网友评论

      本文标题:SpringBoot整合Swagger

      本文链接:https://www.haomeiwen.com/subject/ijizlctx.html