1. 程式人生 > >Spring boot 繼承 Swagger

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;

import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@SpringBootApplication
public class SwaggerStarter {

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

}

 

 

4.   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")
    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;
    }

}

 

 

5.   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;
    }

}

 

 

6. 啟動專案,瀏覽器輸入

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

 

效果如下圖:

 

 

 

.