This is an automated email from the ASF dual-hosted git repository. ningjiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/servicecomb-toolkit.git
commit e32ef38ea107c1947d2fa016307e576d6220e15e Author: kakulisen <[email protected]> AuthorDate: Fri May 24 11:21:19 2019 +0800 Add SpringBoot programming model Signed-off-by: kakulisen <[email protected]> --- .../toolkit/codegen/ServiceCombCodegen.java | 3 + .../libraries/SpringBoot/Application.mustache | 15 +++ .../ServiceComb/libraries/SpringBoot/api.mustache | 31 +++++ .../libraries/SpringBoot/api_test.mustache | 38 +++++++ .../libraries/SpringBoot/bodyParams.mustache | 1 + .../libraries/SpringBoot/formParams.mustache | 2 + .../libraries/SpringBoot/headerParams.mustache | 1 + .../libraries/SpringBoot/operationMethod.mustache | 13 +++ .../libraries/SpringBoot/pathParams.mustache | 1 + .../ServiceComb/libraries/SpringBoot/pom.mustache | 125 +++++++++++++++++++++ .../libraries/SpringBoot/queryParams.mustache | 1 + .../servicecomb/toolkit/codegen/GeneratorTest.java | 1 + .../servicecomb/toolkit/cli/CodeGenerate.java | 2 +- .../apache/servicecomb/toolkit/cli/CliTest.java | 2 +- 14 files changed, 234 insertions(+), 2 deletions(-) diff --git a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java index 668db24..686afaa 100755 --- a/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java +++ b/code-generator/src/main/java/org/apache/servicecomb/toolkit/codegen/ServiceCombCodegen.java @@ -39,6 +39,8 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo private static final String JAX_RS_LIBRARY = "JAX-RS"; + private static final String SPRING_BOOT_LIBRARY = "SpringBoot"; + private String mainClassPackage; private String providerProject = "provider"; @@ -104,6 +106,7 @@ public class ServiceCombCodegen extends AbstractJavaCodegen implements CodegenCo supportedLibraries.put(DEFAULT_LIBRARY, "ServiceComb Server application using the springboot programming model."); supportedLibraries.put(POJO_LIBRARY, "ServiceComb Server application using the pojo programming model."); supportedLibraries.put(JAX_RS_LIBRARY, "ServiceComb Server application using the jax-rs programming model."); + supportedLibraries.put(SPRING_BOOT_LIBRARY, "ServiceComb Server application using the SpringBoot programming model."); setLibrary(DEFAULT_LIBRARY); diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache new file mode 100755 index 0000000..2184ccb --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/Application.mustache @@ -0,0 +1,15 @@ +package {{mainClassPackage}}; + +import org.apache.servicecomb.springboot.starter.provider.EnableServiceComb; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableServiceComb +public class Application { + + public static void main(String[] args) throws Exception { + System.setProperty("local.registry.file", "notExistJustForceLocal"); + SpringApplication.run(Application.class, args); + } +} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache new file mode 100755 index 0000000..33f5a4b --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api.mustache @@ -0,0 +1,31 @@ +package {{apiPackage}}; + +import {{modelPackage}}.*; + +{{#imports}}import {{import}}; +{{/imports}} + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +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 org.apache.servicecomb.provider.rest.common.RestSchema; +import java.util.List; +import java.util.Map; +import static org.springframework.http.MediaType.*; + +@RestSchema(schemaId = "{{#camelcase}}{{classname}}{{/camelcase}}") +@RequestMapping(value = "/", produces = {APPLICATION_JSON_VALUE}) +{{#operations}} +public class {{classname}} { + + {{>operationMethod}} + +} +{{/operations}} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api_test.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api_test.mustache new file mode 100755 index 0000000..d0444c2 --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/api_test.mustache @@ -0,0 +1,38 @@ +package {{package}}; +{{#imports}}import {{import}}; +{{/imports}} +import java.util.*; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import static org.junit.Assert.assertEquals; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class {{classname}}Test { +@Autowired +private {{classname}} api; +{{#operations}} + {{#operation}} + @Test + public void {{operationId}}Test() throws Exception { + {{#allParams}} + {{^isFile}} + {{{dataType}}} {{paramName}} = {{{example}}}; + {{/isFile}} + {{#isFile}} + org.springframework.web.multipart.MultipartFile {{paramName}} = null; + {{/isFile}} + {{/allParams}} + ResponseEntity<{{>returnTypes}}> responseEntity = api.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}); + assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode()); + } + {{/operation}} +{{/operations}} +} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache new file mode 100755 index 0000000..5932bd1 --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/bodyParams.mustache @@ -0,0 +1 @@ +{{#isBodyParam}}@RequestBody {{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache new file mode 100755 index 0000000..336c14d --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/formParams.mustache @@ -0,0 +1,2 @@ +{{#isFormParam}}{{#notFile}} +@RequestPart(value="{{paramName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}@RequestPart("file") MultipartFile {{baseName}}{{/isFile}}{{/isFormParam}} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache new file mode 100755 index 0000000..7c3018b --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/headerParams.mustache @@ -0,0 +1 @@ +{{#isHeaderParam}}@RequestHeader(value="{{paramName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{{dataType}}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache new file mode 100755 index 0000000..18b598c --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/operationMethod.mustache @@ -0,0 +1,13 @@ +{{#operation}} + + @RequestMapping(value = "{{path}}", + {{#hasProduces}}produces = { {{#produces}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}} + {{#hasConsumes}}consumes = { {{#consumes}}"{{mediaType}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}} + method = RequestMethod.{{httpMethod}}) + public ResponseEntity<{{>returnTypes}}> {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, + {{/hasMore}}{{/allParams}}) { + // do some magic! + return new ResponseEntity<{{>returnTypes}}>(HttpStatus.OK); + } + +{{/operation}} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache new file mode 100755 index 0000000..5152be0 --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pathParams.mustache @@ -0,0 +1 @@ +{{#isPathParam}}@PathVariable("{{paramName}}") {{{dataType}}} {{paramName}}{{/isPathParam}} \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache new file mode 100755 index 0000000..3c487c7 --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/pom.mustache @@ -0,0 +1,125 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <parent> + <artifactId>{{artifactId}}</artifactId> + <groupId>{{groupId}}</groupId> + <version>{{artifactVersion}}</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>provider</artifactId> + <packaging>jar</packaging> + <name>provider</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <java-chassis.version>1.2.0</java-chassis.version> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>java-chassis-dependencies</artifactId> + <version>${java-chassis.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + + <dependency> + <groupId>{{groupId}}</groupId> + <artifactId>model</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>spring-boot-starter-provider</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>handler-flowcontrol-qps</artifactId> + </dependency> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>handler-bizkeeper</artifactId> + </dependency> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>handler-tracing-zipkin</artifactId> + </dependency> + <dependency> + <groupId>org.apache.servicecomb</groupId> + <artifactId>inspector</artifactId> + </dependency> + <dependency> + <groupId>org.hibernate.validator</groupId> + <artifactId>hibernate-validator</artifactId> + </dependency> + <dependency> + <groupId>javax.validation</groupId> + <artifactId>validation-api</artifactId> + <version>2.0.1.Final</version> + </dependency> + + </dependencies> + + <!--for package and deploy--> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-jar-plugin</artifactId> + <version>2.6</version> + <configuration> + <archive> + <manifest> + <addClasspath>true</addClasspath> + <classpathPrefix>lib/</classpathPrefix> + <!--change to your main class--> + <mainClass>${package}.Application</mainClass> + </manifest> + <manifestEntries> + <Class-Path>. </Class-Path> + </manifestEntries> + </archive> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>copy-dependencies</id> + <phase>package</phase> + <goals> + <goal>copy-dependencies</goal> + </goals> + <configuration> + <outputDirectory>target/lib</outputDirectory> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>3.1</version> + <configuration> + <source>1.8</source> + <target>1.8</target> + </configuration> + </plugin> + + </plugins> + </build> + +</project> \ No newline at end of file diff --git a/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache new file mode 100755 index 0000000..6ce8e14 --- /dev/null +++ b/code-generator/src/main/resources/ServiceComb/libraries/SpringBoot/queryParams.mustache @@ -0,0 +1 @@ +{{#isQueryParam}}@RequestParam(value = "{{paramName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{{dataType}}} {{paramName}}{{/isQueryParam}} \ No newline at end of file diff --git a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java b/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java index 08f5ae3..e7ae3d4 100755 --- a/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java +++ b/code-generator/src/test/java/org/apache/servicecomb/toolkit/codegen/GeneratorTest.java @@ -40,6 +40,7 @@ public class GeneratorTest { generateCode("SpringMVC"); generateCode("POJO"); generateCode("JAX-RS"); + generateCode("SpringBoot"); } private void generateCode(String programmingModel) throws IOException, URISyntaxException { diff --git a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java b/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java index 4034812..6700110 100755 --- a/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java +++ b/toolkit-cli/src/main/java/org/apache/servicecomb/toolkit/cli/CodeGenerate.java @@ -39,7 +39,7 @@ import io.swagger.codegen.config.CodegenConfigurator; public class CodeGenerate implements Runnable { @Option(name = {"-p", "--programming-model"}, title = "programming model", required = false, - description = "programming model, as SpringMVC, POJO or JAX-RS") + description = "programming model, as SpringMVC, POJO, JAX-RS, and SpringBoot") private String programmingModel; @Option(name = {"-m", "--microservice-framework"}, title = "framework", diff --git a/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java b/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java index b2a7664..cf0d047 100755 --- a/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java +++ b/toolkit-cli/src/test/java/org/apache/servicecomb/toolkit/cli/CliTest.java @@ -31,7 +31,7 @@ public class CliTest { @Test public void generateServiceCombCodeFromSingleContract() throws IOException { - String[] programModels = new String[] {"SpringMVC", "POJO", "JAX-RS"}; + String[] programModels = new String[] {"SpringMVC","POJO","JAX-RS","SpringBoot"}; Path tempDir = Files.createTempDirectory(null); Arrays.stream(programModels).forEach(model -> { try {
