1. 程式人生 > >初次嘗試swagger springmvc整合 生成restful api文件

初次嘗試swagger springmvc整合 生成restful api文件

1、maven 所需jar包

<dependency>
        <groupId>com.mangofactory</groupId>
        <artifactId>swagger-springmvc</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.mangofactory</groupId>
        <artifactId>swagger-models</artifactId>
        <version>1.0.2</version>
    </dependency>
    <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.3.11</version>
    </dependency>
    <!-- swagger-springmvc dependencies -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>15.0</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.4</version>
    </dependency>

2、建立 SwaggerConfig配置檔案


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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
@EnableSwagger
//@EnableWebMvc
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 Title",
                "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;
    }
}

3、配置靜態資源

在 spring-mvc.xml配置靜態資源 <!-- 配置靜態資源 -->
<mvc:resources mapping="/dist/**" location="/WEB-INF/dist/"/>

4、在程式碼中新增相關APIAnnotation

@ResponseBody

@RequestMapping( value = "addUser", method = RequestMethod.POST, produces = "application/json; charset=utf-8")

@ApiOperation(value = "新增使用者", httpMethod =

"POST", response = BaseResultVo.class, notes = "add user")

public String addUser(@ApiParam(required = true, name = "postData", value = "使用者資訊json資料")@RequestParam( value = "postData") String postData,

HttpServletRequest request) {

LOGGER.debug(String.format("at function, %s", postData))if (null == postData || postData.isEmpty())

{returnsuper.buildFailedResultInfo(-1,"post data is empty!");}

UserInfo user = JSON.parseObject(postData, UserInfo.class);

int result = userService.addUser(user);

return buildSuccessResultInfo(result);

}

5、GitHub下載 Swagger UI

到 https://github.com/swagger-api/swagger-ui下載 Swagger ui 注意下載 Swagger 2.0 到 3.0之間的版本 解壓之後 講 dist包全部拷貝到自己專案 WEB-INF目錄下;此時 dist下index.html 頁面為:

6、開啟瀏覽器 輸入 http://localhost/{project}/dist/index.html#



將index.html 的url 修改為 :http://localhost/LCgame/api-docs

重新整理瀏覽器出現

出現 detectedType.toLowerCase is not a function 的js錯誤;網上有幾種說法:

1、說是fastjsonapi問題,升級到1.2.15版本以上就可以解決了 參考:http://blog.csdn.net/nihaoqiulinhe/article/details/68490133

2、不使用mvc:annontation,細化其配置 我試過這兩種方法 都沒用 最後將 swagger-ui.js 6365行進行修改:

重新重新整理瀏覽器:


大功告成!!!