Swagger grails 集成 [英] Swagger grails Integration
问题描述
我是 swagger 的新手,我想使用 grails 框架将 swagger 集成到 Restful API 项目中.如果有人知道我做错了什么,请帮忙?
I am new to swagger and I want to integrate swagger to Restful API project using grails framework. Please help if anybody have any idea what i am doing wrong?
我的 grails 规范如下:
my grails specification as below:
| Grails Version: 3.0.7
| Groovy Version: 2.4.4
| JVM Version: 1.8.0_71
对 swagger 进行了一些设置,如下所示:
Did some settings for swagger as below:
在 build.gradle 中:
in build.gradle:
dependencies {
...
compile "io.swagger:swagger-core:1.5.3"
compile "io.swagger:swagger-jaxrs:1.5.3"
...
}
在 resources.groovy 中
in resources.groovy
import io.swagger.jaxrs.config.BeanConfig
beans = {
swaggerConfig(BeanConfig) {
def serverUrl = "http://localhost:8080/"
def hostName = "localhost:8080"
resourcePackage = "grails.rest.example"
host = hostName
basePath = "/api"
version = 'v0' // Default "1".
title = 'Core Registration API, Version V0'
description = 'API for Accessing secured resources'
contact = 'testtest@mailinator.com'
license = ''
licenseUrl = ''
}
corsFilter(CorsFilter)
}
添加了控制器 ApiDocController.groovy:
Added a Controller ApiDocController.groovy:
package grails.rest.example.apidoc
import grails.web.mapping.LinkGenerator
class ApiDocController {
LinkGenerator grailsLinkGenerator
def apiDocService
def index = {
String basePath = grailsLinkGenerator.serverBaseURL
render(view: 'index', model: [apiDocsPath: "${basePath}/api/swagger-json"])
//render(view: 'index', model: [apiDocsPath: "localhost:8080/api/swagger-json"])
//render(view: 'index', model: [apiDocsPath: "localhost:8080/dist/index.html"])
}
def swaggerJson = {
render apiDocService.generateJSON()
}
}
为控制器添加了一个 URLMapping:
Added a URLMapping for controller:
"/api/info"(controller: 'ApiDoc')
"/"(controller: 'Index')
"500"(controller: 'InternalServerError')
"404"(controller: 'NotFound')
添加了一个服务 ApiDocService.groovy:
Added a service ApiDocService.groovy:
//package com.care.apidoc
package grails.rest.example.apidoc
import io.swagger.jaxrs.config.BeanConfig
import grails.transaction.Transactional
import io.swagger.util.Json
@Transactional
class ApiDocService {
def swaggerConfig
/*
* generates SWAGGer JSON
*/
def generateJSON() {
String[] schemes = ["http"] as String[]
swaggerConfig.setSchemes(schemes)
swaggerConfig.setScan(true)
def swagger = swaggerConfig.getSwagger()
Json.mapper().writeValueAsString(swagger);
}
}
在 src/main/webapp/dist 文件夹中添加 Swagger-ui
added Swagger-ui in src/main/webapp/dist folder
with a working customised API URL "http://localhost:8080/api/orders" in index.html
在 src/main/groovy/CorsFilter.groovy 中添加 CorsFilter 设置
added CorsFilter setting in src/main/groovy/CorsFilter.groovy
import org.springframework.web.filter.OncePerRequestFilter
import javax.annotation.Priority
import javax.servlet.FilterChain
import javax.servlet.ServletException
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
@Priority(Integer.MIN_VALUE)
public class CorsFilter extends OncePerRequestFilter {
public CorsFilter() { }
@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse resp, FilterChain chain)
throws ServletException, IOException {
String origin = req.getHeader("Origin");
boolean options = "OPTIONS".equals(req.getMethod());
resp.addHeader("Access-Control-Allow-Headers", "origin, authorization, accept, content-type, x-requested-with");
resp.addHeader("Access-Control-Allow-Methods", "GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS");
resp.addHeader("Access-Control-Max-Age", "3600");
resp.addHeader("Access-Control-Allow-Origin", origin == null ? "*" : origin);
resp.addHeader("Access-Control-Allow-Credentials", "true");
if (!options) chain.doFilter(req, resp);
}
}
在启动服务器时.但是,当我尝试在 Swagger UI 索引文件中加载 API 时,订单 API 工作正常.表明.规范中没有定义任何操作!
On starting the server. API for orders is working correctly, however, when I try to load the API in Swagger UI index file. it shows. No operations defined in spec!
如附图所示.
推荐答案
你看过 springfox 吗?
这是 Heroku 中托管的 示例 Grails 应用程序,它演示了springfox 与其集成以生成 Open API 规范 2.0 (fka swagger) 中的服务描述的功能.该演示的源代码在此处提供.
Here is a sample Grails Application hosted in Heroku that demonstrates the capabilities of springfox integrating with it to produce the service description in the Open API specification 2.0 (fka swagger). The source code for the demo is available here.
您可以在此处看到此演示现场运行,演示由 grails 应用程序生成并使用 swagger-ui 呈现的 Open API 规范.
You can see this demo running live here demonstrating the Open API specification generated by the grails application and rendered using swagger-ui.
使这成为可能的库是 springfox-grails-integration 库.它即将发布,可能需要做一些工作才能使其成为 grails 插件.有一些关于如何配置库存储库的初步文档.
The library that makes this possible is springfox-grails-integration library. It is about to be released and probably needs a little bit of work to make it a grails plugin. There is some preliminary documentation of how to configure this the library repository.
注意:这只适用于 grails 3.x
NOTE: This only works with grails 3.x
它也是向我们展示你的圣杯 比赛.非常感谢改进此库的反馈.
Also it was a notable library showcased in the SHOW US YOUR GRAILS contest. Feedback to improve this library is much appreciated.
这篇关于Swagger grails 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!