1、springboot配置swagger後自動生成文檔 方便測試html
2、集成步驟(假設springboot已經搭建完成 可運行)java
2.一、引入pom文件web
<!-- 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.二、config類spring
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類json
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主文件中加入掃描api
2.5訪問地址http://localhost:8081/swagger-ui.htmlspringboot