IDEA Swagger 配置

現在的項目大多數都是前後端分離,所以對於後端開發,我們需要一個接口工具幫我們進行接口的整理、也方便我們測試。推薦postMan和Swagger形式,然後postMan需要自己手寫接口,然後參數需要手動粘貼,但是相比於它,Swagger就相對方便很多了,他會根據後臺的代碼,然後自動生成接口以及參數名。

在pom.xml中引入相應的依賴jar包
配置Swagger2配置文件

@Configuration
    @EnableSwagger2
    public class Swagger2 {
    @Bean
    public Docket createRestApi(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.ten.base.controller"))
                .paths(PathSelectors.any()).build();
    }
    
    
    private ApiInfo apiInfo(){
        return  new ApiInfoBuilder().title("Springboot-api文檔")
                .description("").termsOfServiceUrl("")
                .version("1.0").build();
    }

注意:需要將下面紅框中改成自己的的controller路徑,否則swagger頁面不顯示;

package com.example.demowechat.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @ClassName Swagger2
 * @Description TODO
 * @Author 張
 * @Date 2019/07/2809:39
 * Version 1.0
 **/

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi(){
        return  new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()             
                .apis(RequestHandlerSelectors.basePackage
              ("com.example.demowechat.controller")//此出換爲自己的controller路徑
              )
                .paths(PathSelectors.any()).build();
    }


    private ApiInfo apiInfo(){
        return  new ApiInfoBuilder().title("Springboot-api文檔")
                .description("").termsOfServiceUrl("")
                .version("1.0").build();
    }

3.然後在相應接口加上Swagger註解

package com.example.demowechat.controller;

import com.example.demowechat.dataobject.ProductInfo;
import com.example.demowechat.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
/**
 * @ClassName SellProductController  賣家端商品
 * @Author 張
 * @Date 2019/07/3020:53
 * Version 1.0
 **/

@Api(tags = {("賣家端商品列表")})
@Controller
@Slf4j
@RequestMapping("/seller/product")
public class SellProductController {

    @Autowired
    private ProductService productService;


    /**
     *賣家商品列表
     * @param page
     * @param size
     * @param map
     * @return
     */
    @ApiOperation("賣家商品列表")
    @GetMapping("/list")
    public ModelAndView list (@RequestParam(value = "page"  ,defaultValue = "1") Integer page ,
                              @RequestParam(value = "size", defaultValue = "10") Integer size ,
                              Map<String,Object> map){
        PageRequest request = new PageRequest(page -1 ,size);
        Page<ProductInfo>  productInfoPage = productService.findAll(request);
        map.put("productInfoPage",productInfoPage);
        map.put("currentPage",page);
        map.put("size", size);
        return new ModelAndView("product/list" , map);

    }
}

備註:

@Api :用在類上,說明該類的作用
@ApiOperation:用在方法上,說明方法的作用
@ApiModel:描述一個Model的信息
@ApiModelProperty:實體中參數信息

啓動服務測試,在瀏覽器中輸入“http://localhost:8080/swagger-ui.html”,然後我們就可以看到相應的界面了。
在這裏插入圖片描述
5.如果不想手動輸入 地址,可以在idea進行配置一下,就可以自動彈出頁面了。地址填寫:http://127.0.0.1:8080/sell/swagger-ui.html#/
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章