Skip to content

Commit 618d868

Browse files
committed
add java samples
1 parent bf56873 commit 618d868

14 files changed

Lines changed: 975 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
generatorName: spring
2+
outputDir: samples/server/petstore/springboot-sort-validation
3+
library: spring-boot
4+
inputSpec: modules/openapi-generator/src/test/resources/3_0/spring/petstore-sort-validation.yaml
5+
templateDir: modules/openapi-generator/src/main/resources/JavaSpring
6+
additionalProperties:
7+
documentationProvider: none
8+
annotationLibrary: none
9+
useSwaggerUI: "false"
10+
serviceImplementation: "false"
11+
serializableModel: "true"
12+
useBeanValidation: "true"
13+
interfaceOnly: "true"
14+
skipDefaultInterface: "true"
15+
useSpringBoot3: "true"
16+
generateSortValidation: "true"
17+
generatePageableConstraintValidation: "true"
18+
useTags: "true"
19+
requestMappingMode: api_interface
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
README.md
2+
pom.xml
3+
src/main/java/org/openapitools/api/ApiUtil.java
4+
src/main/java/org/openapitools/api/PetApi.java
5+
src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java
6+
src/main/java/org/openapitools/configuration/ValidPageable.java
7+
src/main/java/org/openapitools/configuration/ValidSort.java
8+
src/main/java/org/openapitools/model/Pet.java
9+
src/main/java/org/openapitools/model/PetSort.java
10+
src/main/java/org/openapitools/model/PetSortEnum.java
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.22.0-SNAPSHOT
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# OpenAPI generated API stub
3+
4+
Spring Framework stub
5+
6+
7+
## Overview
8+
This code was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
9+
By using the [OpenAPI-Spec](https://openapis.org), you can easily generate an API stub.
10+
This is an example of building API stub interfaces in Java using the Spring framework.
11+
12+
The stubs generated can be used in your existing Spring-MVC or Spring-Boot application to create controller endpoints
13+
by adding ```@Controller``` classes that implement the interface. Eg:
14+
```java
15+
@Controller
16+
public class PetController implements PetApi {
17+
// implement all PetApi methods
18+
}
19+
```
20+
21+
You can also use the interface to create [Spring-Cloud Feign clients](http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-feign-inheritance).Eg:
22+
```java
23+
@FeignClient(name="pet", url="http://petstore.swagger.io/v2")
24+
public interface PetClient extends PetApi {
25+
26+
}
27+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.openapitools</groupId>
4+
<artifactId>openapi-spring</artifactId>
5+
<packaging>jar</packaging>
6+
<name>openapi-spring</name>
7+
<version>1.0.0</version>
8+
<properties>
9+
<java.version>17</java.version>
10+
<maven.compiler.release>${java.version}</maven.compiler.release>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>3.3.13</version>
17+
<relativePath/> <!-- lookup parent from repository -->
18+
</parent>
19+
20+
<build>
21+
<sourceDirectory>src/main/java</sourceDirectory>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-source-plugin</artifactId>
26+
<executions>
27+
<execution>
28+
<id>attach-sources</id>
29+
<goals>
30+
<goal>jar</goal>
31+
</goals>
32+
</execution>
33+
</executions>
34+
</plugin>
35+
</plugins>
36+
</build>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-web</artifactId>
41+
</dependency>
42+
<dependency>
43+
<groupId>org.springframework.data</groupId>
44+
<artifactId>spring-data-commons</artifactId>
45+
</dependency>
46+
<!-- @Nullable annotation -->
47+
<dependency>
48+
<groupId>com.google.code.findbugs</groupId>
49+
<artifactId>jsr305</artifactId>
50+
<version>3.0.2</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.fasterxml.jackson.datatype</groupId>
54+
<artifactId>jackson-datatype-jsr310</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.openapitools</groupId>
58+
<artifactId>jackson-databind-nullable</artifactId>
59+
<version>0.2.10</version>
60+
</dependency>
61+
<!-- Bean Validation API support -->
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-starter-validation</artifactId>
65+
</dependency>
66+
<dependency>
67+
<groupId>com.fasterxml.jackson.core</groupId>
68+
<artifactId>jackson-databind</artifactId>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.springframework.boot</groupId>
72+
<artifactId>spring-boot-starter-test</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.openapitools.api;
2+
3+
import org.springframework.web.context.request.NativeWebRequest;
4+
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import java.io.IOException;
7+
8+
public class ApiUtil {
9+
public static void setExampleResponse(NativeWebRequest req, String contentType, String example) {
10+
try {
11+
HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class);
12+
if (res != null) {
13+
res.setCharacterEncoding("UTF-8");
14+
res.addHeader("Content-Type", contentType);
15+
res.getWriter().print(example);
16+
}
17+
} catch (IOException e) {
18+
throw new RuntimeException(e);
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)