1. 程式人生 > 實用技巧 >「刷題筆記」LCA問題相關

「刷題筆記」LCA問題相關

1、swagger常用註解說明

2、pom.xml中引用依賴

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

3、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;

/**
 * http://localhost:8090/test/swagger-ui.html
 * swagger2的配置內容僅僅就是需要建立一個Docket例項
 */
@Configuration
@EnableSwagger2 //啟用swagger2
public class Swagger2Config {
//    @Value("${swagger.enable}")
//    private boolean enableSwagger;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .apiInfo(apiInfo())
//                .enable(enableSwagger)//配置是否開啟配置
                .select()
                //@ApiIgnore 這樣,該介面就不會暴露在 swagger2 的頁面下
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//2這裡採用包含註解的方式來確定要顯示的介面
                .apis(RequestHandlerSelectors.basePackage("com.dm.controller")) //2這裡採用包掃描的方式來確定要顯示的介面
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("2服務管理系統")
                .description("2基礎服務包括【2欄位管理模組2】【2介面管理模組2】【2mock服務模組2】【2介面標準化檢測2】四個服務2")
                .version("0.0.1")
                .build(); // 這部分資訊其實可以自定義到配置檔案中讀取
    }
}

4、使用註解後的Controller

import com.dm.model.Result;
import com.dm.model.StatusCode;
import com.dm.repository.entity.tbField;
import com.dm.service.tbFieldService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "tbFieldController",description = "Field相關介面")
@RestController
@RequestMapping("/tbField")
@CrossOrigin
public class tbFieldController {

    @Autowired
    private tbFieldService tbFieldService;

    /***
     * tbField分頁條件搜尋實現
     * @param tbField
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "tbField條件分頁查詢",notes = "分頁條件查詢tbField方法詳情",tags = {"tbFieldController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "當前頁", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每頁顯示條數", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<List<tbField>> findPage(@RequestBody(required = false) @ApiParam(name = "tbField物件",value = "傳入JSON資料",required = false) tbField tbField, @PathVariable  int page, @PathVariable  int size){
        //呼叫tbFieldService實現分頁條件查詢tbField
        List<tbField> tbFieldList = tbFieldService.findPage(tbField, page, size);
        return new Result(StatusCode.OK,"查詢成功",tbFieldList);
    }

    /***
     * tbField分頁搜尋實現
     * @param page:當前頁
     * @param size:每頁顯示多少條
     * @return
     */
    @ApiOperation(value = "tbField分頁查詢",notes = "分頁查詢tbField方法詳情",tags = {"tbFieldController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "當前頁", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每頁顯示條數", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<List<tbField>> findPage(@PathVariable  int page, @PathVariable  int size){
        //呼叫tbFieldService實現分頁查詢tbField
        List<tbField> tbFieldList = tbFieldService.findPage(page, size);
        return new Result<>(StatusCode.OK,"查詢成功",tbFieldList);
    }

    /***
     * 多條件搜尋品牌資料
     * @param tbField
     * @return
     */
    @ApiOperation(value = "tbField條件查詢",notes = "條件查詢tbField方法詳情",tags = {"tbFieldController"})
    @PostMapping(value = "/search" )
    public Result<List<tbField>> findList(@RequestBody(required = false) @ApiParam(name = "tbField物件",value = "傳入JSON資料",required = false) tbField tbField){
        //呼叫tbFieldService實現條件查詢tbField
        List<tbField> list = tbFieldService.findList(tbField);
        return new Result<List<tbField>>(StatusCode.OK,"查詢成功",list);
    }

    /***
     * 根據ID刪除品牌資料
     * @param id
     * @return
     */
    @ApiOperation(value = "tbField根據ID刪除",notes = "根據ID刪除tbField方法詳情",tags = {"tbFieldController"})
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable @ApiParam(value = "主鍵ID", required = true) Integer id){
        //呼叫tbFieldService實現根據主鍵刪除
        tbFieldService.delete(id);
        return new Result(StatusCode.OK,"刪除成功");
    }

    /***
     * 修改tbField資料
     * @param tbField
     * @param id
     * @return
     */
    @ApiOperation(value = "tbField根據ID修改",notes = "根據ID修改tbField方法詳情",tags = {"tbFieldController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主鍵ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "tbField物件",value = "傳入JSON資料",required = false) tbField tbField,@PathVariable Integer id){
        //設定主鍵值
        tbField.setId(id);
        //呼叫tbFieldService實現修改tbField
        tbFieldService.update(tbField);
        return new Result(StatusCode.OK,"修改成功");
    }

    /***
     * 新增tbField資料
     * @param tbField
     * @return
     */
    @ApiOperation(value = "tbField新增",notes = "新增tbField方法詳情",tags = {"tbFieldController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "tbField物件",value = "傳入JSON資料",required = true) tbField tbField){
        //呼叫tbFieldService實現新增tbField
        tbFieldService.add(tbField);
        return new Result(StatusCode.OK,"新增成功");
    }

    /***
     * 根據ID查詢tbField資料
     * @param id
     * @return
     */
    @ApiOperation(value = "tbField根據ID查詢",notes = "根據ID查詢tbField方法詳情",tags = {"tbFieldController"})
    @GetMapping("/{id}")
    public Result<tbField> findById(@PathVariable @ApiParam(value = "主鍵ID", required = true) Integer id){
        //呼叫tbFieldService實現根據主鍵查詢tbField
        tbField tbField = tbFieldService.findById(id);
        return new Result<tbField>(StatusCode.OK,"查詢成功",tbField);
    }

}

5、訪問地址

http://localhost:8090/test/swagger-ui.html