Skip to content

Commit 2754a0d

Browse files
authored
Cloud Run + Cloud Endpoints (GoogleCloudPlatform#3406)
Fixes GoogleCloudPlatform#3095 > It's a good idea to open an issue first for discussion. - [x] I have followed [Sample Format Guide](https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/SAMPLE_FORMAT.md) - [X] `pom.xml` parent set to latest `shared-configuration` - [x] Appropriate changes to README are included in PR - [X] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [X] **Tests** pass: `mvn clean verify` **required** - [X] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [X] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [X] Please **merge** this PR for me once it is approved. <details> <summary>mvn clean verify</summary> <pre> aubre@DESKTOP-F4APIAI ~/Projects/java-docs-samples/run/endpoints-v2-backend (run-endpoints) $ mvn clean verify [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:endpoints >------------------------ [INFO] Building rest 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ endpoints --- [INFO] Deleting /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target [INFO] [INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default) @ endpoints --- [INFO] argLine set to -javaagent:/mnt/c/Users/aubre/.m2/repository/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=/mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target/jacoco.ex ec [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ endpoints --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ endpoints --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ endpoints --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ endpoints --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) @ endpoints --- [INFO] No tests to run. [INFO] [INFO] --- jacoco-maven-plugin:0.8.5:report (report) @ endpoints --- [INFO] Skipping JaCoCo execution due to missing execution data file. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ endpoints --- [INFO] Building jar: /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target/endpoints-1.0-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.3.1.RELEASE:start (pre-integration-test) @ endpoints --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE) 2020-07-22 23:44:57.590 INFO 23566 --- [ main] c.e.endpoints.EndpointsApplication : Starting EndpointsApplication on DESKTOP-F4APIAI with PID 23566 (/mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-bac kend/target/classes started by aubre in /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend) 2020-07-22 23:44:57.597 INFO 23566 --- [ main] c.e.endpoints.EndpointsApplication : No active profile set, falling back to default profiles: default 2020-07-22 23:45:01.012 INFO 23566 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-07-22 23:45:01.042 INFO 23566 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-07-22 23:45:01.042 INFO 23566 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36] 2020-07-22 23:45:01.397 INFO 23566 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-07-22 23:45:01.397 INFO 23566 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3661 ms 2020-07-22 23:45:02.025 INFO 23566 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-07-22 23:45:03.723 INFO 23566 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-07-22 23:45:03.753 INFO 23566 --- [ main] c.e.endpoints.EndpointsApplication : Started EndpointsApplication in 7.509 seconds (JVM running for 9.314) [INFO] [INFO] --- maven-failsafe-plugin:3.0.0-M4:integration-test (default) @ endpoints --- [INFO] No tests to run. [INFO] [INFO] --- springdoc-openapi-maven-plugin:1.0:generate (integration-test) @ endpoints --- 2020-07-22 23:45:06.712 INFO 23566 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2020-07-22 23:45:06.713 INFO 23566 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2020-07-22 23:45:06.728 INFO 23566 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 15 ms 2020-07-22 23:45:07.120 INFO 23566 --- [nio-8080-exec-1] o.springdoc.api.AbstractOpenApiResource : Init duration for springdoc-openapi is: 280 ms [INFO] [INFO] --- openapi-converter-maven-plugin:1.0.0-SNAPSHOT:convertOpenApiDocs (integration-test) @ endpoints --- [INFO] [INFO] --- spring-boot-maven-plugin:2.3.1.RELEASE:stop (post-integration-test) @ endpoints --- [INFO] Stopping application... 2020-07-22 23:45:08.039 INFO 23566 --- [on(2)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested. 2020-07-22 23:45:08.083 INFO 23566 --- [on(2)-127.0.0.1] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' [INFO] [INFO] --- maven-failsafe-plugin:3.0.0-M4:verify (default) @ endpoints --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 38.060 s [INFO] Finished at: 2020-07-22T23:45:08-04:00 [INFO] ------------------------------------------------------------------------ </pre> </details> <details> <summary>mvn -P lint checkstyle:check</summary> <pre> aubre@DESKTOP-F4APIAI ~/Projects/java-docs-samples/run/endpoints-v2-backend (run-endpoints) $ mvn -P lint checkstyle:check [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:endpoints >------------------------ [INFO] Building rest 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-checkstyle-plugin:3.1.1:check (default-cli) @ endpoints --- [INFO] Starting audit... Audit done. [INFO] You have 0 Checkstyle violations. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 12.978 s [INFO] Finished at: 2020-07-22T23:57:47-04:00 [INFO] ------------------------------------------------------------------------ </pre> </details> <details> <summary>mvn -P lint clean compile pmd:cpd-check spotbugs:check</summary> <pre> aubre@DESKTOP-F4APIAI ~/Projects/java-docs-samples/run/endpoints-v2-backend (run-endpoints) $ mvn -P lint clean compile pmd:cpd-check spotbugs:check [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:endpoints >------------------------ [INFO] Building rest 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ endpoints --- [INFO] Deleting /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target [INFO] [INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default) @ endpoints --- [INFO] argLine set to -javaagent:/mnt/c/Users/aubre/.m2/repository/org/jacoco/org.jacoco.agent/0.8.5/org.jacoco.agent-0.8.5-runtime.jar=destfile=/mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target/jacoco.ex ec [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ endpoints --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ endpoints --- ... [INFO] Changes detected - recompiling the module! [INFO] Compiling 2 source files to /mnt/c/Users/aubre/Projects/java-docs-samples/run/endpoints-v2-backend/target/classes [INFO] [INFO] >>> spotbugs-maven-plugin:4.0.0:check (analyze-compile) > :spotbugs @ endpoints >>> [INFO] [INFO] --- spotbugs-maven-plugin:4.0.0:spotbugs (spotbugs) @ endpoints --- ... [INFO] Done SpotBugs Analysis.... [INFO] [INFO] <<< spotbugs-maven-plugin:4.0.0:check (analyze-compile) < :spotbugs @ endpoints <<< [INFO] [INFO] [INFO] --- spotbugs-maven-plugin:4.0.0:check (analyze-compile) @ endpoints --- [INFO] BugInstance size is 2 [INFO] Error size is 0 [INFO] Total bugs: 2 [ERROR] com.example.endpoints.controllers.RepeatController is a Spring endpoint (Controller) [com.example.endpoints.controllers.RepeatController] At RepeatController.java:[lines 34-40] SPRING_ENDPOINT [ERROR] Method com.example.endpoints.controllers.RepeatController.repeat(String, Integer) passes constant String of length 1 to character overridden method [com.example.endpoints.controllers.RepeatController] At RepeatController.jav a:[line 38] UCPM_USE_CHARACTER_PARAMETERIZED_METHOD [INFO] To see bug detail using the Spotbugs GUI, use the following command "mvn spotbugs:gui" [INFO] ------------------------------------------------------------------------ [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:11 min [INFO] Finished at: 2020-07-23T00:00:14-04:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal com.github.spotbugs:spotbugs-maven-plugin:4.0.0:check (analyze-compile) on project endpoints: failed with 2 bugs and 0 errors -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException </pre> </details> ~~This pull request is **not** final. I wanted to get the functional part of the code approved before writing a detailed instructional README.~~
1 parent 08ef8bf commit 2754a0d

File tree

8 files changed

+534
-0
lines changed

8 files changed

+534
-0
lines changed

run/endpoints-v2-backend/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Cloud Run Endpoints Sample
2+
3+
This sample shows how to create a Cloud Endpoints service.
4+
5+
Use it with the [Getting Started with Endpoints for Cloud Run with ESPv2 tutorial][tutorial_link].
6+
7+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README][tutorial_link].
8+
9+
[![Run in Google Cloud][run_img]][run_link]
10+
11+
## Dependencies
12+
13+
* **Spring Boot**: Web server framework.
14+
* **SpringDoc OpenApi**: API documentation generation tool.
15+
* **Jib**: Container build tool.
16+
17+
## Generating the Endpoints spec
18+
19+
20+
1. Update `PROJECT_ID` in `pom.xml` with your GCP Project Id:
21+
22+
```
23+
<properties>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
<maven.compiler.target>11</maven.compiler.target>
26+
<maven.compiler.source>11</maven.compiler.source>
27+
<endpoints.project.id>PROJECT ID</endpoints.project.id>
28+
</properties>
29+
```
30+
31+
1. Update `<CLOUD_RUN_HOSTNAME>` and `<CLOUD_RUN_SERVICE_URL>` in `EndpointsApplication.java` according to variable
32+
33+
definitions at [Getting Started with Endpoints for Cloud Run with ESPv2](https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-run#reserve_hostname)
34+
(Reserving a Cloud Run hostname).
35+
36+
1. Run `mvn clean install` (or alternatively just `mvn verify`) to use the SpringDoc Maven Plugin to generate a base
37+
openapi file in the root directory.
38+
39+
You are able to customize the output filename by changing its plugin configuration `outputFileName` in pom.xml.
40+
41+
SpringDoc Openapi Maven Plugin runs during the integration test lifecycle phase in order to pull down a Swagger 3.0
42+
openapi specification and automatically write into a file.
43+
44+
1. Manually, you must switch the generated file's swagger version from 3.0 to 2.0. At the end, it should look like the
45+
file openapi-run.yaml in this demo project. If you are using an IDE like IntelliJ IDEA Ultimate, after switching the
46+
version number to 2.0, it should tell you what's wrong with the file.
47+
48+
1. Follow the [Getting Started with Endpoints for Cloud Run with ESPv2](https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-run#deploy_configuration)
49+
guide (Deploying the Endpoints configuration).
50+
51+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
52+
[run_link]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/markdown-preview
53+
[tutorial_link]: https://cloud.google.com/endpoints/docs/openapi/get-started-cloud-run
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
swagger: "2.0"
15+
info:
16+
title: Cloud Endpoints + Cloud Run
17+
description: Sample API on Cloud Endpoints with a Cloud Run backend
18+
version: 1.0.0
19+
paths:
20+
/api/v1/repeat:
21+
get:
22+
tags:
23+
- repeat-controller
24+
operationId: repeat
25+
parameters:
26+
- name: text
27+
in: query
28+
required: true
29+
type: string
30+
- name: times
31+
in: query
32+
required: true
33+
type: integer
34+
responses:
35+
"200":
36+
description: OK
37+
produces:
38+
- application/json
39+
x-google-backend:
40+
protocol: h2
41+
address: <CLOUD_RUN_SERVICE_URL>
42+
schemes:
43+
- https
44+
host: <CLOUD_RUN_HOSTNAME>

run/endpoints-v2-backend/pom.xml

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2020 Google LLC
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
15+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
16+
<modelVersion>4.0.0</modelVersion>
17+
<groupId>com.example</groupId>
18+
<artifactId>endpoints</artifactId>
19+
<packaging>jar</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<name>rest</name>
22+
23+
<!--
24+
The parent pom defines common style checks and testing strategies for our samples.
25+
Removing or replacing it should not affect the execution of the samples in anyway.
26+
-->
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.0.18</version>
31+
</parent>
32+
33+
<properties>
34+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
35+
<maven.compiler.target>11</maven.compiler.target>
36+
<maven.compiler.source>11</maven.compiler.source>
37+
<endpoints.project.id>PROJECT ID</endpoints.project.id>
38+
</properties>
39+
40+
<dependencies>
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot</artifactId>
44+
<version>2.3.1.RELEASE</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-autoconfigure</artifactId>
49+
<version>2.3.1.RELEASE</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.springframework</groupId>
53+
<artifactId>spring-web</artifactId>
54+
<version>5.2.7.RELEASE</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-web</artifactId>
59+
<version>2.3.1.RELEASE</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.fasterxml.jackson.core</groupId>
63+
<artifactId>jackson-databind</artifactId>
64+
<version>2.11.1</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>io.swagger.core.v3</groupId>
68+
<artifactId>swagger-jaxrs2</artifactId>
69+
<version>2.1.2</version>
70+
</dependency>
71+
<dependency>
72+
<groupId>io.swagger.core.v3</groupId>
73+
<artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
74+
<version>2.1.2</version>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.springdoc</groupId>
78+
<artifactId>springdoc-openapi-ui</artifactId>
79+
<version>1.4.3</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.projectlombok</groupId>
83+
<artifactId>lombok</artifactId>
84+
<version>1.18.12</version>
85+
<scope>provided</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.junit.jupiter</groupId>
89+
<artifactId>junit-jupiter-api</artifactId>
90+
<version>5.6.2</version>
91+
<scope>test</scope>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.springframework.boot</groupId>
95+
<artifactId>spring-boot-starter-test</artifactId>
96+
<version>2.3.3.RELEASE</version>
97+
<scope>test</scope>
98+
</dependency>
99+
</dependencies>
100+
101+
<build>
102+
<plugins>
103+
<plugin>
104+
<groupId>org.springframework.boot</groupId>
105+
<artifactId>spring-boot-maven-plugin</artifactId>
106+
<version>2.3.1.RELEASE</version>
107+
<executions>
108+
<execution>
109+
<id>pre-integration-test</id>
110+
<goals>
111+
<goal>start</goal>
112+
</goals>
113+
</execution>
114+
<execution>
115+
<id>post-integration-test</id>
116+
<goals>
117+
<goal>stop</goal>
118+
</goals>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
<plugin>
123+
<groupId>com.google.cloud.tools</groupId>
124+
<artifactId>jib-maven-plugin</artifactId>
125+
<version>2.4.0</version>
126+
<configuration>
127+
<to>
128+
<image>gcr.io/${endpoints.project.id}/endpoints-container</image>
129+
</to>
130+
</configuration>
131+
</plugin>
132+
<plugin>
133+
<groupId>org.springdoc</groupId>
134+
<artifactId>springdoc-openapi-maven-plugin</artifactId>
135+
<version>1.0</version>
136+
<executions>
137+
<execution>
138+
<id>integration-test</id>
139+
<goals>
140+
<goal>generate</goal>
141+
</goals>
142+
</execution>
143+
</executions>
144+
<configuration>
145+
<apiDocsUrl>http://localhost:8080/v3/api-docs.yaml</apiDocsUrl>
146+
<outputFileName>openapi-run.yaml</outputFileName>
147+
<outputDir>${project.basedir}</outputDir>
148+
</configuration>
149+
</plugin>
150+
</plugins>
151+
</build>
152+
153+
</project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.endpoints;
18+
19+
import io.swagger.v3.oas.models.OpenAPI;
20+
import io.swagger.v3.oas.models.info.Info;
21+
import java.util.Collections;
22+
import java.util.Map;
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.context.annotation.Bean;
26+
27+
@SpringBootApplication
28+
public class EndpointsApplication {
29+
30+
public static void main(String[] args) {
31+
SpringApplication.run(EndpointsApplication.class, args);
32+
}
33+
34+
@Bean
35+
public OpenAPI customOpenApi() {
36+
return new OpenAPI()
37+
.info(new Info()
38+
.title("Cloud Endpoints + Cloud Run")
39+
.description("Sample API on Cloud Endpoints with a Cloud Run backend")
40+
.version("1.0.0"))
41+
.servers(null)
42+
.extensions(Map.of(
43+
"host", "<CLOUD_RUN_HOSTNAME>",
44+
"schemes", Collections.singletonList("https"),
45+
"produces", Collections.singletonList("application/json"),
46+
"x-google-backend", Map.of(
47+
"address", "<CLOUD_RUN_SERVICE_URL>",
48+
"protocol", "h2"
49+
)));
50+
}
51+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.endpoints.controllers;
18+
19+
import org.springframework.http.HttpStatus;
20+
import org.springframework.http.ResponseEntity;
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.RequestMapping;
23+
import org.springframework.web.bind.annotation.RequestParam;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
27+
@RequestMapping("/api/v1")
28+
@RestController
29+
public class RepeatController {
30+
31+
@GetMapping("/repeat")
32+
public ResponseEntity<String> repeat(@RequestParam("text") String text,
33+
@RequestParam("times") Integer times) {
34+
StringBuilder response = new StringBuilder();
35+
for (int i = 0; i < times - 1; i++) {
36+
response.append(text).append(", ");
37+
}
38+
response.append(text).append("!");
39+
40+
return new ResponseEntity<>(response.toString(), HttpStatus.OK);
41+
}
42+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
server:
15+
port: ${PORT:8080}

0 commit comments

Comments
 (0)