diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java index 446185bcd47..23ffe6c0fc4 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringMVCServerCodegen.java @@ -18,12 +18,13 @@ public class SpringMVCServerCodegen extends JavaClientCodegen implements Codegen public static final String CONFIG_PACKAGE = "configPackage"; protected String title = "Petstore Server"; protected String configPackage = ""; + protected String templateFileName = "api.mustache"; public SpringMVCServerCodegen() { super(); outputFolder = "generated-code/javaSpringMVC"; modelTemplateFiles.put("model.mustache", ".java"); - apiTemplateFiles.put("api.mustache", ".java"); + apiTemplateFiles.put(templateFileName, ".java"); embeddedTemplateDir = templateDir = "JavaSpringMVC"; apiPackage = "io.swagger.api"; modelPackage = "io.swagger.model"; @@ -51,6 +52,11 @@ public class SpringMVCServerCodegen extends JavaClientCodegen implements Codegen ); cliOptions.add(new CliOption(CONFIG_PACKAGE, "configuration package for generated code")); + + supportedLibraries.clear(); + supportedLibraries.put(DEFAULT_LIBRARY, "Default Spring MVC server stub."); + supportedLibraries.put("j8-async", "Use async servlet feature and Java 8's default interface. Generating interface with service " + + "declaration is useful when using Maven plugin. Just provide a implementation with @Controller to instantiate service."); } public CodegenType getTag() { @@ -168,6 +174,12 @@ public class SpringMVCServerCodegen extends JavaClientCodegen implements Codegen } } } + if("j8-async".equals(getLibrary())) { + apiTemplateFiles.remove(this.templateFileName); + this.templateFileName = "api-j8-async.mustache"; + apiTemplateFiles.put(this.templateFileName, ".java"); + } + return objs; } diff --git a/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api-j8-async.mustache b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api-j8-async.mustache new file mode 100755 index 00000000000..b11e5c2db20 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/JavaSpringMVC/api-j8-async.mustache @@ -0,0 +1,64 @@ +package {{apiPackage}}; + +import {{modelPackage}}.*; + +{{#imports}}import {{import}}; +{{/imports}} + +import java.util.concurrent.Callable; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Authorization; +import io.swagger.annotations.AuthorizationScope; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +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.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; + +import static org.springframework.http.MediaType.*; + +@Controller +@RequestMapping(value = "/{{baseName}}", produces = {APPLICATION_JSON_VALUE}) +@Api(value = "/{{baseName}}", description = "the {{baseName}} API") +{{>generatedAnnotation}} +{{#operations}} +public interface {{classname}} { + {{#operation}} + + @ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = { + {{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = { + {{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}}, + {{/hasMore}}{{/scopes}} + }{{/isOAuth}}){{#hasMore}}, + {{/hasMore}}{{/authMethods}} + }{{/hasAuthMethods}}) + @ApiResponses(value = { {{#responses}} + @ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},{{/hasMore}}{{/responses}} }) + @RequestMapping(value = "{{path}}", + {{#hasProduces}}produces = { {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}} + {{#hasConsumes}}consumes = { {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}} + method = RequestMethod.{{httpMethod}}) + default CallablereturnTypes}}>> {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, + {{/hasMore}}{{/allParams}}) + throws NotFoundException { + // do some magic! + return () -> new ResponseEntity<{{>returnTypes}}>(HttpStatus.OK); + } + + {{/operation}} +} +{{/operations}}