/openapi-springboot-sample

springboot microserver sample by generating with openapi-generator

Primary LanguageHTMLApache License 2.0Apache-2.0

openapi-springboot-sample

springboot microserver sample by generating with openapi-generator

How to build

to generate source code with open-generator from spec/openapi.yaml

$ gradle clean openApiGenerate

to show help related to openApiGenerate

$ gradle openApiGenerators

generated code is in build/generated

$ tree build/generated/
build/generated/
├── README.md
├── pom.xml
└── src
    └── main
        ├── java
        │   └── org
        │       └── openapitools
        │           ├── api
        │           │   ├── ApiUtil.java
        │           │   ├── BooksApi.java
        │           │   ├── BooksApiController.java
        │           │   ├── PingApi.java
        │           │   ├── PingApiController.java
        │           │   ├── StreamApi.java
        │           │   ├── StreamApiController.java
        │           │   ├── UsersApi.java
        │           │   └── UsersApiController.java
        │           ├── configuration
        │           │   ├── HomeController.java
        │           │   └── OpenAPIDocumentationConfig.java
        │           ├── invoker
        │           │   ├── OpenAPI2SpringBoot.java
        │           │   └── RFC3339DateFormat.java
        │           └── model
        │               ├── Book.java
        │               ├── Format.java
        │               ├── Frequency.java
        │               ├── GenericError.java
        │               ├── InlineResponse200.java
        │               ├── InlineResponse201.java
        │               ├── StreamFormat.java
        │               └── User.java
        └── resources
            └── application.properties

10 directories, 24 files

How to boot

$ gradle bootRun

or

$ gradle build
$ java -jar build/libs/openapi-springboot-sample.jar

(but there is no data, just confirm that generated jar can boot up)

request to localhost

$ gradle bootRun

ping

$ curl -s -v -X GET localhost:8080/ping | jq .
{
  "message": "pong"
}

list

$ curl -s -v -X GET -H 'Auth-Token: secret' localhost:8080/users/list | jq .

create

$ curl -s -v -X POST -H 'Auth-Token: passwd' -H 'Content-Type: application/json' localhost:8080/users/create -d '{"id": 4, "username": "sala", "phone": "000-123-456", "birthday": "2010-04-01"}' | jq .
{
  "id": 4
}

retrieve

$ curl -s -v -X GET -H 'Auth-Token: passwd' localhost:8080/users/4 | jq .
{
  "id": 4,
  "username": "sala",
  "firstName": null,
  "lastName": null,
  "birthday": "2010-04-01",
  "email": null,
  "password": null,
  "phone": "000-123-456",
  "userStatus": null
}

update

$ curl -s -v -X PUT -H 'Auth-Token: passwd' -H 'Content-Type: application/json' localhost:8080/users/4 -d '{"firstName": "sala", "lastName": "smith"}' | jq .
< HTTP/1.1 200 OK
$ curl -s -v -X GET -H 'Auth-Token: passwd' localhost:8080/users/4 | jq .
{
  "id": 4,
  "username": null,
  "firstName": "sala",
  "lastName": "smith",
  "birthday": null,
  "email": null,
  "password": null,
  "phone": null,
  "userStatus": null
}

delete

$ curl -s -v -X DELETE -H 'Auth-Token: passwd' localhost:8080/users/4 | jq .
< HTTP/1.1 200 OK
$ curl -s -v -X GET -H 'Auth-Token: passwd' localhost:8080/users/4 | jq .
< HTTP/1.1 404 Not Found

stream

$ curl -s -v "localhost:8080/stream/byte?format=csv"
$ curl -s -v "localhost:8080/stream/input?format=csv"

References