1. 程式人生 > >Spring Web MVC專案搭建(二)Swagger搭建

Spring Web MVC專案搭建(二)Swagger搭建

  1. Maven引入swagger相關jar包
<dependency>
    <groupId>com.mangofactory</groupId>
    <artifactId>swagger-springmvc</artifactId>
    <version>1.0.2</version>
</dependency>

2.建立config類

package com.love.yu.config;

import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.mangofactory.swagger.configuration.SpringSwaggerConfig; import com.mangofactory.swagger.models.dto.ApiInfo; import com.mangofactory.swagger.plugin.EnableSwagger; import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin; @Configuration
@EnableSwagger public class SwaggerConfig { private SpringSwaggerConfig springSwaggerConfig; /** * Required to autowire SpringSwaggerConfig */ @Autowired public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) { this.springSwaggerConfig = springSwaggerConfig; } /** * Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc * framework - allowing for multiple swagger groups i.e. same code base * multiple swagger resource listings. */
@Bean public SwaggerSpringMvcPlugin customImplementation() { return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(".*?"); } private ApiInfo apiInfo() { ApiInfo apiInfo = new ApiInfo("我的小專案", "My Apps API Description", "My Apps API terms of service", "My Apps API Contact Email", "My Apps API Licence Type", "My Apps API License URL"); return apiInfo; } }

如果spring不能掃描到該類,就要手動建立bean

<bean  class="com.love.yu.config.SwaggerConfig"/>

3.修改介面處的註解

package com.love.yu.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;

@RestController
@Api(value = "MyController", description = "測試介面")
public class MyController {

    @RequestMapping(path = "/test")
    @ApiResponses({ @ApiResponse(code = 200, message = "成功"), @ApiResponse(code = 400, message = "失敗") })
    @ApiOperation(value = "測試", httpMethod = "GET", notes = "測試")
    public String myTest(@ApiParam(name = "yourname", value = "姓名")@RequestParam(value="yourname",required=true) String yourname) {
        return "歡迎你" + yourname;
    }
}

4.引入SwaggerUI元件
在swagger-ui官網中找到github專案,將dist資料夾下的檔案考到自己專案的webapp的swagger資料夾內,開啟index.html檔案,修改url為ip地址/專案名稱/api-docs

    url: "http://127.0.0.1:8088/yu-rest/api-docs",

4.啟動專案測試,會發現報錯

java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
...
java.lang.ClassNotFoundException: com.fasterxml.jackson.core.util.DefaultIndenter

缺少相應jar包,所以在maven中依賴它

<dependency>        
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.9</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.8.9</version>
</dependency>
@RequestMapping(path = "/test", produces = {"text/html;charset=UTF-8","application/json;"})

這次再請求介面,得到正確結果

這裡寫圖片描述

最基本的Swagger搭建就已經成功了
其實這兩次寫部落格都是大概寫了下搭建流程,沒有附帶些理論性知識,是因為最近每天加班,回來再做個飯,陪妹紙玩幾把王者榮耀就十一點了,時間太緊;而且我對理論部分也沒有很深入的瞭解,現在只是串起來簡單的專案搭建流程,慢慢的會抽空補全每一步或者每一個問題的原理或原因,我也是個初級小碼農,慢慢的在利用部落格把自己學的東西記錄下來,也會試著建立自己的GitHub,一點點的提升自己,最後,今天再見啦~~~北京大雨,我要洗洗睡啦