springboot配置swagger

一、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主文件中加入扫描

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

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

转载于:https://my.oschina.net/u/3234821/blog/3062240

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值