1. 程式人生 > >springboot swagger 整合

springboot swagger 整合

conf swagger div autowire api接口 handlers ppi 方法 set

Swagger 是一個規範和完整的框架,用於生成、描述、調用和可視化 RESTful 風格的 Web 服務。 文件的方法,參數和模型緊密集成到服務器端的代碼,允許API來始終保持同步。

作用:

1. 接口的文檔在線自動生成。

2. 功能測試。

1.添加pom.xml

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

2.在啟動類同級增加類

package com;

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.taiji.demo.controller")) .paths(PathSelectors.any()) .build(); } //構建 api文檔的詳細信息函數,註意這裏的註解引用的是哪個 private ApiInfo apiInfo() { return new ApiInfoBuilder() //頁面標題 .title("系統API接口") //創建人 .contact(new Contact("zhangyg", "www.redxun.com", "[email protected]")) //版本號 .version("1.0") //描述 .description("系統API接口") .build(); } }

3.編輯java 文件

package com.taiji.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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.RestController;

import com.alibaba.fastjson.JSONObject;
import com.taiji.core.util.IdUtil;
import com.taiji.demo.model.SaleOrder;
import com.taiji.demo.service.SaleOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;


@RestController
@Api(value="銷售訂單API接口")
public class SaleOrderController {
        
        
        @Autowired
        private SaleOrderService saleOrderService;
         
    
        @ApiOperation(value="添加訂單",notes="添加訂單描述")
        @RequestMapping(value="/add",method={RequestMethod.POST,RequestMethod.GET})
        public void add(@ApiParam(value = "用戶數據", 
                        required = true,
                        type="JSONObject",
                        example="{\"name\":\"A\",\"userId\":\"B\",\"total\":\"100\"}") @RequestBody JSONObject json){
            SaleOrder order=new SaleOrder();
            order.setId(IdUtil.getId());
            order.setName(json.getString("name"));
            order.setCreator(json.getString("userId"));
            order.setTotal(json.getDouble("total"));
            saleOrderService.create(order);
        }
        
        @ApiOperation(value="根據ID查看明細",notes="根據ID查看明細")
        @ApiImplicitParams(value={
            @ApiImplicitParam(name="id",value="訂單ID",required=true,dataType="String")    
        })
        @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
        public SaleOrder get(@PathVariable(value="id") String id){
            SaleOrder order= saleOrderService.get(id);
            return order;
        }
        
        @RequestMapping(value="/page/{page}",method=RequestMethod.GET)
        @ApiResponses({
            @ApiResponse(response=JSONObject.class, code = 200, message = "訂單列表")
        })
        public JSONObject page(@PathVariable(value="page") int page){
            JSONObject order= saleOrderService.getAll(page,10);
            return order;
        }
}

4.訪問平臺列表

http://localhost:8000/demo/swagger-ui.html#/sale45order45controller

技術分享圖片

5.在線調試接口

技術分享圖片

輸入好參數後,點擊try it out 按鈕,可以對代碼進行調試。

springboot swagger 整合