GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Cloud Run + Cloud Endpoints (#3406) · jrgito/java-docs-samples@2754a0d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • run/endpoints-v2-backend
    • README.md
    • openapi-run.yaml
    • pom.xml
    • src
      • main
        • java/com/example/endpoints
          • EndpointsApplication.java
          • controllers
            • RepeatController.java
        • resources
          • application.yml
      • test/java/com/example/controllers
        • RepeatControllerIT.java
        • RepeatControllerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎run/endpoints-v2-backend/README.md‎

Copy file name to clipboard
+53Lines changed: 53 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎run/endpoints-v2-backend/openapi-run.yaml‎

Copy file name to clipboard
+44Lines 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>
Collapse file

‎run/endpoints-v2-backend/pom.xml‎

Copy file name to clipboard
+153Lines 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>
Collapse file

‎run/endpoints-v2-backend/src/main/java/com/example/endpoints/EndpointsApplication.java‎

Copy file name to clipboard
+51Lines 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+
}
Collapse file

‎run/endpoints-v2-backend/src/main/java/com/example/endpoints/controllers/RepeatController.java‎

Copy file name to clipboard
+42Lines 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+
}
Collapse file

‎run/endpoints-v2-backend/src/main/resources/application.yml‎

Copy file name to clipboard
+15Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.