一、springboot配置swagger后自动生成文档 方便测试

二、集成步骤(假设springboot已经搭建完成 可运行)

2.1、引入pom文件

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






2.2、config类

package com.mao.swagger.config;

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

import io.swagger.annotations.ApiOperation;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }

}



2.3controller类


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.domain.rest;
import com.example.demo.service.HttpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @title: HttpController
 * @projectName bootdemo
 * @description: TODO
 * @date 2019/6/1322:19
 */

@Controller
@Api(description = "swagger接口")
@RequestMapping(value = "/test")
public class HttpController {


    @Autowired
    private HttpService httpService;


    @Autowired
    private RestTemplate restTemplate;


    /**
     *
     * @return
     */

    @ApiOperation(value = "测试Swager" ,  notes="查询接口")
    @PostMapping(value = "testSwagger",produces = "application/json;charts=UTF-8")
    public JSON test_Swager()
    {
        String urlApi = "http://www.nmc.cn/f/rest/province";

        Map<String, Object> paramMap = new HashMap<>();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);


        ResponseEntity<String> entity = restTemplate.getForEntity(urlApi, String.class);

        JSONObject j=new JSONObject();
        rest r=new rest("2","3","4");
        j.put("cont",r);
        j.put("lsit", entity.getBody());
        return j;
    }







}



2.4在springboot主文件中加入扫描

springboot配置swagger-LMLPHP

2.5访问地址http://localhost:8081/swagger-ui.html

springboot配置swagger-LMLPHP

参考:https://www.cnblogs.com/mao2080/p/8991027.html

06-14 23:13