Skip to content

Commit 6b2d3a8

Browse files
api first tutorial
1 parent 912277c commit 6b2d3a8

5 files changed

Lines changed: 421 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.devonfw.quarkus.petstore.rest.v1;
2+
3+
import java.util.Collections;
4+
import java.util.LinkedHashMap;
5+
import java.util.List;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
import javax.validation.Valid;
10+
11+
import com.devonfw.quarkus.openapi.petstore.domain.Pet;
12+
import com.devonfw.quarkus.openapi.petstore.rest.v1.PetsApi;
13+
14+
public class PetstoreRestService implements PetsApi {
15+
16+
private Set<Pet> pets = Collections.newSetFromMap(Collections.synchronizedMap(new LinkedHashMap<>()));
17+
18+
@Override
19+
public void createPets(@Valid Pet pet) {
20+
21+
pet.setId(Long.valueOf(this.pets.size()));
22+
this.pets.add(pet);
23+
}
24+
25+
@Override
26+
public List<Pet> listPets(Integer limit) {
27+
28+
if (limit == null)
29+
return this.pets.stream().collect(Collectors.toList());
30+
return this.pets.stream().limit(limit).collect(Collectors.toList());
31+
}
32+
33+
@Override
34+
public Pet showPetById(String petId) {
35+
36+
List<Pet> petList = this.pets.stream().filter(pet -> pet.getId() == Long.valueOf(petId))
37+
.collect(Collectors.toList());
38+
if (petList.size() == 0)
39+
return null;
40+
return petList.get(0);
41+
}
42+
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Finally, test the REST service.
2+
3+
Open a new terminal and type in the folling command to make a POST request and create a new pet:
4+
5+
curl -X POST -H "Content-Type: application/json" -d '{"name": "Alex", "tag": "Dog"}' http://localhost:8080/pets
6+
7+
After executing the command, there should be a pet stored in the list.
8+
Let's check this by getting the entire list using the `listPets` method of the REST service.
9+
10+
Open https://[[HOST_SUBDOMAIN]]-8080-[[KATACODA_HOST]].environments.katacoda.com/pets in the browser and see if the pet has been saved.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
paths:
10+
/pets:
11+
get:
12+
summary: List all pets
13+
operationId: listPets
14+
tags:
15+
- pets
16+
parameters:
17+
- name: limit
18+
in: query
19+
description: How many items to return at one time (max 100)
20+
required: false
21+
schema:
22+
type: integer
23+
format: int32
24+
responses:
25+
'200':
26+
description: A paged array of pets
27+
headers:
28+
x-next:
29+
description: A link to the next page of responses
30+
schema:
31+
type: string
32+
content:
33+
application/json:
34+
schema:
35+
$ref: "#/components/schemas/Pets"
36+
default:
37+
description: unexpected error
38+
content:
39+
application/json:
40+
schema:
41+
$ref: "#/components/schemas/Error"
42+
post:
43+
summary: Create a pet
44+
operationId: createPets
45+
tags:
46+
- pets
47+
requestBody:
48+
content:
49+
application/json:
50+
schema:
51+
$ref: '#/components/schemas/Pet'
52+
responses:
53+
'201':
54+
description: Null response
55+
default:
56+
description: unexpected error
57+
content:
58+
application/json:
59+
schema:
60+
$ref: "#/components/schemas/Error"
61+
/pets/{petId}:
62+
get:
63+
summary: Info for a specific pet
64+
operationId: showPetById
65+
tags:
66+
- pets
67+
parameters:
68+
- name: petId
69+
in: path
70+
required: true
71+
description: The id of the pet to retrieve
72+
schema:
73+
type: string
74+
responses:
75+
'200':
76+
description: Expected response to a valid request
77+
content:
78+
application/json:
79+
schema:
80+
$ref: "#/components/schemas/Pet"
81+
default:
82+
description: unexpected error
83+
content:
84+
application/json:
85+
schema:
86+
$ref: "#/components/schemas/Error"
87+
components:
88+
schemas:
89+
Pet:
90+
type: object
91+
required:
92+
- id
93+
- name
94+
properties:
95+
id:
96+
type: integer
97+
format: int64
98+
name:
99+
type: string
100+
tag:
101+
type: string
102+
Pets:
103+
type: array
104+
items:
105+
$ref: "#/components/schemas/Pet"
106+
Error:
107+
type: object
108+
required:
109+
- code
110+
- message
111+
properties:
112+
code:
113+
type: integer
114+
format: int32
115+
message:
116+
type: string

api-first-approach/files/pom.xml

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.devonfw.quarkus</groupId>
6+
<artifactId>api-first-tutorial</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<properties>
9+
<compiler-plugin.version>3.8.1</compiler-plugin.version>
10+
<maven.compiler.release>11</maven.compiler.release>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
13+
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
14+
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
15+
<quarkus.platform.version>2.6.1.Final</quarkus.platform.version>
16+
<surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
17+
</properties>
18+
<dependencyManagement>
19+
<dependencies>
20+
<dependency>
21+
<groupId>${quarkus.platform.group-id}</groupId>
22+
<artifactId>${quarkus.platform.artifact-id}</artifactId>
23+
<version>${quarkus.platform.version}</version>
24+
<type>pom</type>
25+
<scope>import</scope>
26+
</dependency>
27+
</dependencies>
28+
</dependencyManagement>
29+
<dependencies>
30+
<dependency>
31+
<groupId>io.quarkus</groupId>
32+
<artifactId>quarkus-resteasy-jackson</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.quarkus</groupId>
36+
<artifactId>quarkus-arc</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>io.quarkus</groupId>
40+
<artifactId>quarkus-resteasy</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>io.quarkus</groupId>
44+
<artifactId>quarkus-junit5</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.rest-assured</groupId>
49+
<artifactId>rest-assured</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<groupId>${quarkus.platform.group-id}</groupId>
57+
<artifactId>quarkus-maven-plugin</artifactId>
58+
<version>${quarkus.platform.version}</version>
59+
<extensions>true</extensions>
60+
<executions>
61+
<execution>
62+
<goals>
63+
<goal>build</goal>
64+
<goal>generate-code</goal>
65+
<goal>generate-code-tests</goal>
66+
</goals>
67+
</execution>
68+
</executions>
69+
</plugin>
70+
<plugin>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>${compiler-plugin.version}</version>
73+
<configuration>
74+
<compilerArgs>
75+
<arg>-parameters</arg>
76+
</compilerArgs>
77+
</configuration>
78+
</plugin>
79+
<plugin>
80+
<artifactId>maven-surefire-plugin</artifactId>
81+
<version>${surefire-plugin.version}</version>
82+
<configuration>
83+
<systemPropertyVariables>
84+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
85+
<maven.home>${maven.home}</maven.home>
86+
</systemPropertyVariables>
87+
</configuration>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
<profiles>
92+
<profile>
93+
<id>native</id>
94+
<activation>
95+
<property>
96+
<name>native</name>
97+
</property>
98+
</activation>
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<artifactId>maven-failsafe-plugin</artifactId>
103+
<version>${surefire-plugin.version}</version>
104+
<executions>
105+
<execution>
106+
<goals>
107+
<goal>integration-test</goal>
108+
<goal>verify</goal>
109+
</goals>
110+
<configuration>
111+
<systemPropertyVariables>
112+
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
113+
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
114+
<maven.home>${maven.home}</maven.home>
115+
</systemPropertyVariables>
116+
</configuration>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
<properties>
123+
<quarkus.package.type>native</quarkus.package.type>
124+
</properties>
125+
</profile>
126+
127+
<profile>
128+
<id>apigen</id>
129+
<activation><activeByDefault>true</activeByDefault></activation>
130+
<build>
131+
<plugins>
132+
<plugin>
133+
<groupId>org.openapitools</groupId>
134+
<artifactId>openapi-generator-maven-plugin</artifactId>
135+
<version>5.3.1</version>
136+
<executions>
137+
<execution>
138+
<phase>generate-sources</phase>
139+
<goals>
140+
<goal>generate</goal>
141+
</goals>
142+
<configuration>
143+
<inputSpec>${project.basedir}/src/main/resources/petstore-api.yaml</inputSpec>
144+
<generatorName>jaxrs-spec</generatorName>
145+
<apiPackage>com.devonfw.quarkus.openapi.petstore.rest.v1</apiPackage>
146+
<modelPackage>com.devonfw.quarkus.openapi.petstore.domain</modelPackage>
147+
<configOptions>
148+
<sourceFolder>src/main/gen</sourceFolder>
149+
<useSwaggerAnnotations>false</useSwaggerAnnotations>
150+
<useTags>true</useTags>
151+
<interfaceOnly>true</interfaceOnly>
152+
<generatePom>true</generatePom>
153+
</configOptions>
154+
</configuration>
155+
</execution>
156+
</executions>
157+
</plugin>
158+
</plugins>
159+
</build>
160+
</profile>
161+
</profiles>
162+
</project>

0 commit comments

Comments
 (0)