Skip to content

Commit e0315b9

Browse files
committed
improve sample
1 parent f1935b4 commit e0315b9

20 files changed

Lines changed: 1710 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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: "true"
11+
serializableModel: "true"
12+
beanValidations: "true"
13+
useSpringBoot3: "true"
14+
generateSortValidation: "true"
15+
generatePageableConstraintValidation: "true"
16+
useTags: "true"
17+
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
README.md
2+
pom.xml
3+
src/main/java/org/openapitools/OpenApiGeneratorApplication.java
4+
src/main/java/org/openapitools/RFC3339DateFormat.java
5+
src/main/java/org/openapitools/api/ApiUtil.java
6+
src/main/java/org/openapitools/api/PetApi.java
7+
src/main/java/org/openapitools/configuration/EnumConverterConfiguration.java
8+
src/main/java/org/openapitools/configuration/HomeController.java
9+
src/main/java/org/openapitools/configuration/ValidPageable.java
10+
src/main/java/org/openapitools/configuration/ValidSort.java
11+
src/main/java/org/openapitools/model/Pet.java
12+
src/main/java/org/openapitools/model/PetSort.java
13+
src/main/resources/application.properties
14+
src/main/resources/openapi.yaml
15+
src/test/java/org/openapitools/OpenApiGeneratorApplicationTests.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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# OpenAPI generated server
2+
3+
Spring Boot Server
4+
5+
## Overview
6+
This server was generated by the [OpenAPI Generator](https://openapi-generator.tech) project.
7+
By using the [OpenAPI-Spec](https://openapis.org), you can easily generate a server stub.
8+
This is an example of building a OpenAPI-enabled server in Java using the SpringBoot framework.
9+
10+
11+
Start your server as a simple java application
12+
Change default port value in application.properties
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.springframework.boot</groupId>
25+
<artifactId>spring-boot-maven-plugin</artifactId>
26+
<configuration>
27+
</configuration>
28+
</plugin>
29+
</plugins>
30+
</build>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.data</groupId>
38+
<artifactId>spring-data-commons</artifactId>
39+
</dependency>
40+
<!-- @Nullable annotation -->
41+
<dependency>
42+
<groupId>com.google.code.findbugs</groupId>
43+
<artifactId>jsr305</artifactId>
44+
<version>3.0.2</version>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.fasterxml.jackson.datatype</groupId>
48+
<artifactId>jackson-datatype-jsr310</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.openapitools</groupId>
52+
<artifactId>jackson-databind-nullable</artifactId>
53+
<version>0.2.10</version>
54+
</dependency>
55+
<!-- Bean Validation API support -->
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-validation</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.fasterxml.jackson.core</groupId>
62+
<artifactId>jackson-databind</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-test</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
</dependencies>
70+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.openapitools;
2+
3+
import com.fasterxml.jackson.databind.Module;
4+
import org.openapitools.jackson.nullable.JsonNullableModule;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.ComponentScan;
9+
import org.springframework.context.annotation.FilterType;
10+
import org.springframework.context.annotation.FullyQualifiedAnnotationBeanNameGenerator;
11+
12+
@SpringBootApplication(
13+
nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class
14+
)
15+
@ComponentScan(
16+
basePackages = {"org.openapitools", "org.openapitools.api" , "org.openapitools.configuration"},
17+
nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class
18+
)
19+
public class OpenApiGeneratorApplication {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(OpenApiGeneratorApplication.class, args);
23+
}
24+
25+
@Bean(name = "org.openapitools.OpenApiGeneratorApplication.jsonNullableModule")
26+
public Module jsonNullableModule() {
27+
return new JsonNullableModule();
28+
}
29+
30+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.openapitools;
2+
3+
import com.fasterxml.jackson.databind.util.StdDateFormat;
4+
5+
import java.text.DateFormat;
6+
import java.text.FieldPosition;
7+
import java.text.ParsePosition;
8+
import java.util.Date;
9+
import java.util.GregorianCalendar;
10+
import java.util.TimeZone;
11+
12+
public class RFC3339DateFormat extends DateFormat {
13+
private static final long serialVersionUID = 1L;
14+
private static final TimeZone TIMEZONE_Z = TimeZone.getTimeZone("UTC");
15+
16+
private final StdDateFormat fmt = new StdDateFormat()
17+
.withTimeZone(TIMEZONE_Z)
18+
.withColonInTimeZone(true);
19+
20+
public RFC3339DateFormat() {
21+
this.calendar = new GregorianCalendar();
22+
}
23+
24+
@Override
25+
public Date parse(String source, ParsePosition pos) {
26+
return fmt.parse(source, pos);
27+
}
28+
29+
@Override
30+
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
31+
return fmt.format(date, toAppendTo, fieldPosition);
32+
}
33+
34+
@Override
35+
public Object clone() {
36+
return this;
37+
}
38+
}
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)