1. 程式人生 > >SpringBoot配置Swagger例項(POST接收json引數)

SpringBoot配置Swagger例項(POST接收json引數)

工程目錄結構:

 

 

首先,引入jar包,只需要以下兩個即可

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.4.0</version>
</dependency>

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.4
.0</version> </dependency>

 

1、Swagger配置類

package com.mark.swagger;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public class Swagger2 { //swagger2的配置檔案,這裡可以配置swagger2的一些基本的內容,比如掃描的包等等 @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //為當前包路徑 .apis(RequestHandlerSelectors.basePackage("com.mark"
)) .paths(PathSelectors.any()) .build(); } //構建 api文件的詳細資訊函式,注意這裡的註解引用的是哪個 private ApiInfo apiInfo() { return new ApiInfoBuilder() //頁面標題 .title("Spring Boot 測試使用 Swagger2 構建RESTful API") //建立人 .contact(new Contact("WangXiaodong", "http://www.baidu.com", "")) //版本號 .version("1.0") //描述 .description("API 描述") .build(); } }

注意修改:basePackage("介面所在controller的包路徑")

 

 2、實體類

 

介面接收入參為json串,需由實體類轉化為對應key-value

package com.mark.swagger;

public class User {

    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User [name=" + name + "]";
    }
    
}

 

3、介面所在Controller

 

package com.mark.swagger;

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.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/hello")
@Api(value = "/hello")
public class SwagController {
 
 
    @ApiOperation(value = "查詢使用者", notes = "查詢使用者")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/getName",method=RequestMethod.POST)
    @ResponseBody
    public User getName(@RequestBody User name) {
        System.out.println(name.toString());
        return name;
    }
 
 
}

 

 通過@RequestBody註解,將接收到的json引數,轉化為實體類User對應的值

 

 4、SpringBoot啟動類

 雖然大家都知道,還是來一個吧

package com.mark;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

 

 

 測試

輸入地址:localhost:8080/swagger-ui.html

 

如下所示:出現剛才寫的介面

 

 

 點選SHow/Hide

1、/hello/getName:路徑

2、Model:入參json對應的model,就是上面的User

3、填寫要傳入的引數(json格式)

 

 

 

傳入引數:

{
  "name": "WangXiaodong"
}

 

 測試結果: