Spring boot 集成 Swagger

 

添加依赖包

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

 

开启注解

@EnableSwagger2

 

 

示例如下:

1.   新建 Maven 项目 swagger

 

2.   pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java</groupId>
    <artifactId>swagger</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>


    <!-- 配置版本常量 -->
    <properties>
        <jdk.version>1.8</jdk.version>
    </properties>

    <dependencies>

        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>


    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

3.   SwaggerStarter.java

package com.java.swagger;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SwaggerStarter {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerStarter.class, args);
    }

}

 

 

4.   SwaggerConfig.java

package com.java.swagger.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.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiSelectorBuilder builder = docket.apiInfo(apiInfo()).select();
        builder.apis(RequestHandlerSelectors.basePackage("com.java.swagger.controller"));
        builder.paths(PathSelectors.any());
        return builder.build();
    }

    private ApiInfo apiInfo() {
        ApiInfoBuilder builder = new ApiInfoBuilder();
        builder.title("我的项目Swagger");
        builder.description("简单易用的API文档");
        builder.version("V1.0");
        return builder.build();
    }

}

 

 

5.   User.java

package com.java.swagger.pojo;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

import io.swagger.annotations.ApiModelProperty;

public class User {

    @ApiModelProperty("用户名")
    private String name;

    @ApiModelProperty("年龄")
    private int age;

    @ApiModelProperty(value = "生日", example = "2018-12-31 10:00:00")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date birthDay;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

}

 

 

6.   UserController.java

package com.java.swagger.controller;

import java.util.Calendar;
import java.util.Date;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.swagger.pojo.User;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/getUesr/{name}")
    @ApiOperation(value = "查询用户信息", notes = "各种解释说明信息")
    public User getUser(@ApiParam("用户名") @PathVariable String name) {
        User user = new User();
        user.setName(name);
        user.setAge(18);
        user.setBirthDay(new Date());
        return user;
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户", notes = "各种解释说明信息")
    public User addUser(@ApiParam("用户信息") @RequestBody User user) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(user.getBirthDay());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        user.setBirthDay(calendar.getTime());

        user.setAge(user.getAge() + 1);
        return user;
    }

}

 

 

7. 启动项目,浏览器输入

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

 

效果如下图:

 

 

 

 

 

.

posted @ 2019-01-14 20:44  诚信天下  阅读(336)  评论(0编辑  收藏  举报