updated templates

This commit is contained in:
Tony Tam 2015-03-31 21:47:30 -07:00
parent 084d448fd4
commit f142474a54
8 changed files with 88 additions and 66 deletions

View File

@ -10,12 +10,12 @@ import java.util.*;
import java.io.File; import java.io.File;
public class SpringfoxServerCodegen extends JavaClientCodegen implements CodegenConfig { public class SpringfoxServerCodegen extends JavaClientCodegen implements CodegenConfig {
protected String invokerPackage = "com.concur.service.api"; protected String invokerPackage = "io.swagger.api";
protected String groupId = "com.concur.service"; protected String groupId = "io.swagger";
protected String artifactId = "swagger-server"; protected String artifactId = "swagger-server";
protected String artifactVersion = "1.0.0"; protected String artifactVersion = "1.0.0";
protected String sourceFolder = "src/main/java"; protected String sourceFolder = "src/main/java";
protected String title = "Concur Server"; protected String title = "Petstore Server";
protected String configPackage = ""; protected String configPackage = "";
@ -37,9 +37,9 @@ public class SpringfoxServerCodegen extends JavaClientCodegen implements Codegen
modelTemplateFiles.put("model.mustache", ".java"); modelTemplateFiles.put("model.mustache", ".java");
apiTemplateFiles.put("api.mustache", ".java"); apiTemplateFiles.put("api.mustache", ".java");
templateDir = "JavaSpringfox"; templateDir = "JavaSpringfox";
apiPackage = "com.concur.service.api"; apiPackage = "io.swagger.api";
modelPackage = "com.concur.service.model"; modelPackage = "io.swagger.model";
configPackage = "com.concur.service.configuration"; configPackage = "io.swagger.configuration";
additionalProperties.put("invokerPackage", invokerPackage); additionalProperties.put("invokerPackage", invokerPackage);

View File

@ -1,40 +1,28 @@
package {{configPackage}}; package {{configPackage}};
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import springdox.documentation.swagger2.annotations.EnableSwagger2;
import springdox.documentation.spring.web.plugins.DocumentationConfigurer;
import springdox.documentation.spi.DocumentationType;
import springdox.documentation.service.ApiInfo;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.service.ApiInfo;
import java.util.Set; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration @Configuration
@ComponentScan(basePackages = "{{apiPackage}}")
@EnableWebMvc @EnableWebMvc
@EnableSwagger2 //Loads the spring beans required by the framework @EnableSwagger2 //Loads the spring beans required by the framework
public class SwaggerConfig { public class SwaggerConfig {
private DocumentationConfigurer documentationConfigurer;
@Autowired
public void setDocumentationConfigurer(DocumentationConfigurer configurer){
this.documentationConfigurer = documentationConfigurer;
}
@Bean @Bean
ApiInfo apiInfo() { ApiInfo apiInfo() {
ApiInfo apiInfo = new ApiInfo( ApiInfo apiInfo = new ApiInfo(
"{{appName}}", "{{appName}}",
"{{appDescription}}", "{{appDescription}}",
"1.0.0", "{{appVersion}}",
"My Apps API terms of service", "{{infoUrl}}",
"{{infoEmail}}", "{{infoEmail}}",
"{{licenseInfo}}", "{{licenseInfo}}",
"{{licenseUrl}}" ); "{{licenseUrl}}" );
@ -42,14 +30,8 @@ private DocumentationConfigurer documentationConfigurer;
} }
@Bean @Bean
public DocumentationConfigurer customImplementation(){ public Docket customImplementation(){
return new DocumentationConfigurer(DocumentationType.SWAGGER_2) return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
.groupName("default")
.includePatterns(".*replace {{appName}} with your api classes.*")
.apiInfo(apiInfo());
} }
@Bean
ObjectMapper objectMapper() { return new ObjectMapper(); }
} }

View File

@ -2,49 +2,50 @@ package {{apiPackage}};
import {{modelPackage}}.*; import {{modelPackage}}.*;
import com.wordnik.swagger.annotations.*;
import com.sun.jersey.multipart.FormDataParam;
{{#imports}}import {{import}}; {{#imports}}import {{import}};
{{/imports}} {{/imports}}
import java.util.List; import com.wordnik.swagger.annotations.Api;
import {{package}}.NotFoundException; import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import javax.ws.rs.core.Response; import com.wordnik.swagger.annotations.ApiResponse;
import javax.ws.rs.*; import com.wordnik.swagger.annotations.ApiResponses;
import java.io.InputStream;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import static org.springframework.http.MediaType.*; import static org.springframework.http.MediaType.*;
@Controller @Controller
@RequestMapping("/{{baseName}}",produces = {APPLICATION_JSON_VALUE}) @RequestMapping(value = "/{{baseName}}", produces = {APPLICATION_JSON_VALUE})
@Api(value = "/{{baseName}}", description = "the {{baseName}} API") @Api(value = "/{{baseName}}", description = "the {{baseName}} API")
{{#operations}} {{#operations}}
public class {{classname}} { public class {{classname}} {
{{#operation}} {{#operation}}
{{#subresourceOperation}}@RequestMapping(value = "{{path}}", method = RequestMethod.{{httpMethod}} ){{/subresourceOperation}}
{{#hasConsumes}}@Consumes({ {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }){{/hasConsumes}}
{{#hasProduces}}@Produces({ {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }){{/hasProduces}}
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}) @ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
@ApiResponses(value = { {{#responses}} @ApiResponses(value = { {{#responses}}
@ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}}, @ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},{{/hasMore}}{{/responses}} })
{{/hasMore}}{{/responses}} }) @RequestMapping(value = "{{path}}",
{{#hasProduces}}produces = { {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}
public Response {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{#hasConsumes}}consumes = { {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}
method = RequestMethod.{{httpMethod}})
public ResponseEntity<{{returnType}}> {{nickname}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},
{{/hasMore}}{{/allParams}}) {{/hasMore}}{{/allParams}})
throws NotFoundException { throws NotFoundException {
// do some magic! // do some magic!
return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build(); return new ResponseEntity<{{returnType}}>(HttpStatus.OK);
} }
{{/operation}} {{/operation}}

View File

@ -1,2 +1,2 @@
{{#isFormParam}}{{#notFile}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}})@FormParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@ApiParam(value = "{{{description}}}") @FormDataParam("file") InputStream inputStream, {{#isFormParam}}{{#notFile}}
@ApiParam(value = "file detail") @FormDataParam("file") FormDataContentDisposition fileDetail{{/isFile}}{{/isFormParam}} @ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}})@RequestPart("{{paramName}}") {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@ApiParam(value = "file detail") @RequestPart("file") MultipartFile fileDetail{{/isFile}}{{/isFormParam}}

View File

@ -1 +1 @@
{{#isHeaderParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}})@HeaderParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/isHeaderParam}} {{#isHeaderParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}})@RequestHeader("{{paramName}}") {{{dataType}}} {{paramName}}{{/isHeaderParam}}

View File

@ -1 +1 @@
{{#isPathParam}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}} {{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @PathParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/isPathParam}} {{#isPathParam}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}} {{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @PathVariable("{{paramName}}") {{{dataType}}} {{paramName}}{{/isPathParam}}

View File

@ -33,7 +33,7 @@
<webAppConfig> <webAppConfig>
<contextPath>{{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}}</contextPath> <contextPath>{{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}}</contextPath>
</webAppConfig> </webAppConfig>
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory> <webAppSourceDirectory>target/${project.artifactId}-${project-version}</webAppSourceDirectory>
<webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml> <webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml>
<stopPort>8079</stopPort> <stopPort>8079</stopPort>
<stopKey>stopit</stopKey> <stopKey>stopit</stopKey>
@ -105,6 +105,45 @@
<version>${jersey-version}</version> <version>${jersey-version}</version>
</dependency> </dependency>
<!--Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!--SpringFox dependencies-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-core</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spi</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-web</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.scalatest</groupId> <groupId>org.scalatest</groupId>
<artifactId>scalatest_2.9.1</artifactId> <artifactId>scalatest_2.9.1</artifactId>
@ -125,11 +164,9 @@
</dependencies> </dependencies>
<repositories> <repositories>
<repository> <repository>
<id>sonatype-snapshots</id> <id>jcenter-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url> <name>jcenter</name>
<snapshots> <url>http://oss.jfrog.org/artifactory/oss-snapshot-local/</url>
<enabled>true</enabled>
</snapshots>
</repository> </repository>
</repositories> </repositories>
<properties> <properties>
@ -140,5 +177,7 @@
<scala-test-version>1.6.1</scala-test-version> <scala-test-version>1.6.1</scala-test-version>
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<servlet-api-version>2.5</servlet-api-version> <servlet-api-version>2.5</servlet-api-version>
<springfox-version>2.0.0-SNAPSHOT</springfox-version>
<spring-version>4.0.9.RELEASE</spring-version>
</properties> </properties>
</project> </project>

View File

@ -1 +1 @@
{{#isQueryParam}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @QueryParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}} {{#isQueryParam}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestParam("{{paramName}}") {{{dataType}}} {{paramName}}{{/isQueryParam}}