2016-09-21 1 views
8

swagger-codegen을 사용하여 REST 클라이언트 및 정적 HTML 문서를 생성하려고합니다.gradle을 사용하여 swagger.json을 생성하는 방법은 무엇입니까?

그러나 swagger-codegen은 swagger.json을 입력해야합니다.

나는 Swagger가 장착 된 실행중인 REST 서버에서이를 얻을 수 있음을 알고 있습니다.

그러나 웹 컨테이너에서 응용 프로그램을 실행할 필요없이 브라우저에서 swagger.json을 Java 코드에서 직접 가져올 수 있습니다 (예 : 소스 코드에서 gradle로 생성). 그것?

+0

아직 조사 중입니다. – tbsalling

+0

https://github.com/gigaSproule/swagger-gradle-plugin 이 플러그인을 사용해 보셨습니까? 그것은 당신이 요구하는 것을 정확하게 수행한다고 주장합니다. –

+0

swagger-gradle-plugin 사용시 다음 오류가 발생합니다. com.fasterxml.jackson.databind.JsonMappingException : [소스 : 알 수 없음]에서 입력의 끝으로 인해 매핑 할 내용이 없습니다. 줄 : 1, 열 : 0] – lex

답변

1

주요 아이디어는 buildScript에서, Gradle을이 같은 것을 사용할 수 있도록하기위한 클래스 패스에 자신감-받는다는 - 플러그인과 자바 클래스를 추가하는 것입니다

buildscript { 
    repositories { 
     mavenCentral() 
    } 

    dependencies { 
     classpath files(project(':swagger-maven-example').configurations['runtime'].files) 
     classpath files(project(':swagger-maven-example').sourceSets['main'].output.classesDir) 
    } 
} 

을 어디에서 첫 번째 줄 종속성은 하위 프로젝트에서 swagger libs를 가져오고 두 번째 줄은 스와거 주석을 포함해야하는 클래스를 가져옵니다.

이 후에는 간단한 자바 클래스로 Gradle을에 받는다는 플러그인을 호출 할 수 있습니다

// a trick to have all needed classes in the classpath 
def customClass = new GroovyClassLoader() 

buildscript.configurations.classpath.each { 
    // println it.toURI().toURL() 
    customClass.addURL(it.toURI().toURL()) 
} 

final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo',true, customClass).newInstance(
     apiSources: [ 
       new ApiSource(
         springmvc: false, 
         locations: ['com/github/kongchen/swagger/sample/wordnik/resource'], 
         schemes: ['http', 'https'], 
         host: 'petstore.swagger.wordnik.com', 
         basePath: '/api', 
         info: new Info(
           title: 'Swagger Maven Plugin Sample', 
           version: 'v1', 
           description: 'This is a sample for swagger-maven-plugin', 
           termsOfService: 'http://www.github.com/kongchen/swagger-maven-plugin', 
           contact: new Contact(
             email: '[email protected]', 
             name: 'Kong Chen', 
             url: 'http://kongch.com' 
           ), 
           license: new License(
             url: 'http://www.apache.org/licenses/LICENSE-2.0.html', 
             name: 'Apache 2.0' 
           ) 
         ), 
         outputPath: file("${buildDir}/swagger/document.html").path, 
         swaggerDirectory: file("${buildDir}/swagger/swagger-ui").path, 
         templatePath: file("${project(':swagger-maven-example').projectDir}/templates/strapdown.html.hbs") 
       ) 
     ] 
) 

// maven plugin 
mavenTask.execute() 

Here이 예를 찾을 수 있습니다.

2

이 조금 오래 그러나 나는 한마디로 내가 가진 연구를 시작했습니다 ... 정확히 같은 궁금 해서요 : 최소한의 REST API를 노출

나는 두 개의 서로 다른 접근 방식을 사용하여 빌드 유물로 JSON 사양을 생성 할 수 있었다.

  • (어쨌든 서버를 시작하기 때문에 이것이 중요한지 확실하지 않음) 사양을 생성하는 통합 테스트 (Spring의 mock MVC)를 실행한다. here에서 아이디어를 빌 렸습니다.
  • 간략한 프로젝트에 내 연구를 요약했습니다. here. Automation 섹션을 참조하십시오. 코드와 예제가 포함되어 있습니다.

    관련 문제