forked from loafle/openapi-generator-original
Update Vert.x Web template to Vert.x 4 (#7364)
* Update Vert.x Web template to Vert.x 4 Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Bad pom Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Bad pom 2 Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Stick to Java 8 Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Latest vert.x 4 Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
ea559b5e20
commit
80bef2f79a
@ -1,7 +1,7 @@
|
||||
generatorName: java-vertx-web
|
||||
outputDir: samples/server/petstore/java-vertx-web/rx
|
||||
outputDir: samples/server/petstore/java-vertx-web
|
||||
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore.yaml
|
||||
templateDir: modules/openapi-generator/src/main/resources/JavaVertXWebServer
|
||||
additionalProperties:
|
||||
hideGenerationTimestamp: "true"
|
||||
artifactId: java-vertx-web-rx-server
|
||||
artifactId: java-vertx-web-server
|
@ -17,19 +17,14 @@
|
||||
package org.openapitools.codegen.languages;
|
||||
|
||||
import io.swagger.v3.oas.models.media.Schema;
|
||||
import org.openapitools.codegen.CodegenConstants;
|
||||
import org.openapitools.codegen.CodegenModel;
|
||||
import org.openapitools.codegen.CodegenOperation;
|
||||
import org.openapitools.codegen.CodegenProperty;
|
||||
import org.openapitools.codegen.CodegenType;
|
||||
import org.openapitools.codegen.*;
|
||||
import org.openapitools.codegen.meta.GeneratorMetadata;
|
||||
import org.openapitools.codegen.meta.Stability;
|
||||
import org.openapitools.codegen.SupportingFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class JavaVertXWebServerCodegen extends AbstractJavaCodegen {
|
||||
|
||||
@ -69,7 +64,7 @@ public class JavaVertXWebServerCodegen extends AbstractJavaCodegen {
|
||||
updateOption(CodegenConstants.API_PACKAGE, apiPackage);
|
||||
updateOption(CodegenConstants.MODEL_PACKAGE, modelPackage);
|
||||
updateOption(CodegenConstants.INVOKER_PACKAGE, invokerPackage);
|
||||
updateOption(this.DATE_LIBRARY, this.getDateLibrary());
|
||||
updateOption(DATE_LIBRARY, this.getDateLibrary());
|
||||
|
||||
// Override type mapping
|
||||
typeMapping.put("file", "FileUpload");
|
||||
@ -99,6 +94,7 @@ public class JavaVertXWebServerCodegen extends AbstractJavaCodegen {
|
||||
importMapping.put("JsonProperty", "com.fasterxml.jackson.annotation.JsonProperty");
|
||||
importMapping.put("JsonValue", "com.fasterxml.jackson.annotation.JsonValue");
|
||||
importMapping.put("FileUpload", "io.vertx.ext.web.FileUpload");
|
||||
importMapping.put("JsonObject", "io.vertx.core.json.JsonObject");
|
||||
|
||||
modelDocTemplateFiles.clear();
|
||||
apiDocTemplateFiles.clear();
|
||||
@ -107,10 +103,7 @@ public class JavaVertXWebServerCodegen extends AbstractJavaCodegen {
|
||||
supportingFiles.clear();
|
||||
supportingFiles.add(new SupportingFile("supportFiles/openapi.mustache", resourceFolder, "openapi.yaml"));
|
||||
supportingFiles.add(new SupportingFile("supportFiles/HttpServerVerticle.mustache", sourcePackageFolder, "HttpServerVerticle.java"));
|
||||
supportingFiles.add(new SupportingFile("supportFiles/MainVerticle.mustache", sourcePackageFolder, "MainVerticle.java"));
|
||||
supportingFiles.add(new SupportingFile("supportFiles/ApiResponse.mustache", sourcePackageFolder, "ApiResponse.java"));
|
||||
supportingFiles.add(new SupportingFile("supportFiles/ApiException.mustache", sourcePackageFolder, "ApiException.java"));
|
||||
supportingFiles.add(new SupportingFile("supportFiles/ParameterCast.mustache", sourcePackageFolder, "ParameterCast.java"));
|
||||
supportingFiles.add(new SupportingFile("supportFiles/pom.mustache", "", "pom.xml"));
|
||||
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")
|
||||
@ -129,6 +122,7 @@ public class JavaVertXWebServerCodegen extends AbstractJavaCodegen {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
|
||||
Map<String, Object> newObjs = super.postProcessOperationsWithModels(objs, allModels);
|
||||
@ -141,11 +135,39 @@ public class JavaVertXWebServerCodegen extends AbstractJavaCodegen {
|
||||
if (operation.returnType == null) {
|
||||
operation.returnType = "Void";
|
||||
}
|
||||
if (operation.allParams.stream().anyMatch(p -> p.isFormParam && p.isFile)) {
|
||||
// If there is a file upload, exclude other form params since it's not clear how the user should access to these
|
||||
operation.allParams = operation
|
||||
.allParams
|
||||
.stream()
|
||||
.filter(p -> !p.isFormParam || p.isFile)
|
||||
.collect(Collectors.toList());
|
||||
} else if (operation.allParams.stream().anyMatch(p -> p.isFormParam)) {
|
||||
// In Vert.x 4 Web OpenAPI the forms are handled as single json object
|
||||
// We create a dummy param here and remove the other ones
|
||||
CodegenParameter dummyParam = new CodegenParameter();
|
||||
dummyParam.isFormParam = true;
|
||||
dummyParam.isFile = false;
|
||||
dummyParam.dataType = "JsonObject";
|
||||
dummyParam.paramName = "formBody";
|
||||
operation.allParams = Stream.concat(
|
||||
operation.allParams.stream().filter(p -> !p.isFormParam),
|
||||
Stream.of(dummyParam)
|
||||
).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
return newObjs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessParameter(CodegenParameter parameter) {
|
||||
super.postProcessParameter(parameter);
|
||||
if (parameter.isUuid || parameter.isDate || parameter.isDateTime) {
|
||||
parameter.dataType = "String";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
|
||||
generateYAMLSpecFile(objs);
|
||||
|
@ -7,8 +7,15 @@ Project generated on : {{generatedDate}}
|
||||
This document assumes you have maven available.
|
||||
|
||||
To build the project using maven, run:
|
||||
|
||||
```bash
|
||||
mvn package && java -jar target/target/java-vertx-web-rx-server-{{artifactVersion}}-fat.jar
|
||||
mvn package
|
||||
```
|
||||
|
||||
To run the project, run the jar or use maven exec plugin:
|
||||
|
||||
```bash
|
||||
mvn exec:java
|
||||
```
|
||||
|
||||
If all builds successfully, the server should run on [http://localhost:8080/](http://localhost:8080/)
|
||||
|
@ -5,7 +5,8 @@ package {{package}};
|
||||
|
||||
import {{invokerPackage}}.ApiResponse;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -13,7 +14,7 @@ import java.util.Map;
|
||||
public interface {{classname}} {
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
Single<ApiResponse<{{{returnType}}}>> {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}},{{/hasMore}}{{/allParams}});
|
||||
Future<ApiResponse<{{{returnType}}}>> {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
}
|
||||
|
@ -3,66 +3,61 @@ package {{package}};
|
||||
{{#imports}}import {{import}};
|
||||
{{/imports}}
|
||||
|
||||
import {{invokerPackage}}.ParameterCast;
|
||||
import {{invokerPackage}}.ApiException;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.core.json.jackson.DatabindCodec;
|
||||
import io.vertx.ext.web.openapi.RouterFactory;
|
||||
import io.vertx.ext.web.validation.RequestParameters;
|
||||
import io.vertx.ext.web.validation.RequestParameter;
|
||||
import io.vertx.ext.web.validation.ValidationHandler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class {{classname}}Handler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger({{classname}}Handler.class);
|
||||
private {{classname}} apiImpl = new {{classname}}Impl();
|
||||
|
||||
public {{classname}}Handler(Map<String, Handler<RoutingContext>> operationHandlers) {
|
||||
private final {{classname}} apiImpl;
|
||||
|
||||
public {{classname}}Handler() {
|
||||
this.apiImpl = new {{classname}}Impl();
|
||||
}
|
||||
|
||||
public void mount(RouterFactory factory) {
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
operationHandlers.put("{{operationId}}", this::{{operationId}});
|
||||
factory.operation("{{operationId}}").handler(this::{{operationId}});
|
||||
{{/operation}}
|
||||
{{/operations}}
|
||||
}
|
||||
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
|
||||
private void {{operationId}}(RoutingContext routingContext) {
|
||||
logger.info("{{operationId}}()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
{{#returnType}}Single{{/returnType}}{{^returnType}}Completable{{/returnType}}.defer( () -> {
|
||||
{{#allParams}}{{^isBodyParam}}{{>headerParams}}{{>pathParams}}{{>queryParams}}{{>formParams}}{{/isBodyParam}}{{/allParams}}
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
{{#allParams}}{{>headerParams}}{{>pathParams}}{{>queryParams}}{{>formParams}}{{>bodyParams}}{{/allParams}}
|
||||
{{#allParams}}
|
||||
{{#isBodyParam}}
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
{{{dataType}}} {{paramName}} = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<{{{dataType}}}>(){});
|
||||
{{/isBodyParam}}
|
||||
logger.debug("Parameter {{paramName}} is {}", {{paramName}});
|
||||
{{/allParams}}
|
||||
{{#allParams}}
|
||||
logger.info("Parameter {{paramName}} is {}", {{paramName}});
|
||||
{{/allParams}}
|
||||
return apiImpl.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
|
||||
apiImpl.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
routingContext.response().end();
|
||||
}
|
||||
}).dispose();
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
{{/operation}}
|
||||
|
@ -4,9 +4,10 @@ package {{package}};
|
||||
{{/imports}}
|
||||
|
||||
import {{invokerPackage}}.ApiResponse;
|
||||
import {{invokerPackage}}.ApiException;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.handler.impl.HttpStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -16,8 +17,8 @@ import java.util.Map;
|
||||
public class {{classname}}Impl implements {{classname}} {
|
||||
{{#operations}}
|
||||
{{#operation}}
|
||||
public Single<ApiResponse<{{{returnType}}}>> {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
public Future<ApiResponse<{{{returnType}}}>> {{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
{{/operation}}
|
||||
|
4
modules/openapi-generator/src/main/resources/JavaVertXWebServer/bodyParams.mustache
vendored
Normal file
4
modules/openapi-generator/src/main/resources/JavaVertXWebServer/bodyParams.mustache
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{{#isBodyParam}}
|
||||
RequestParameter body = requestParameters.body();
|
||||
{{{dataType}}} {{paramName}} = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isBodyParam}}
|
@ -1,18 +1,9 @@
|
||||
{{#isFormParam}}
|
||||
{{^isListContainer}}
|
||||
{{^isFile}}
|
||||
{{#isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.toObject(routingContext.request().getFormAttribute("{{baseName}}"), {{dataType}}.class);
|
||||
{{/isModel}}
|
||||
{{^isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.to{{dataType}}(routingContext.request().getFormAttribute("{{baseName}}"));
|
||||
{{/isModel}}
|
||||
RequestParameter body = requestParameters.body();
|
||||
JsonObject {{paramName}} = body != null ? body.getJsonObject() : null;
|
||||
{{/isFile}}
|
||||
{{#isFile}}
|
||||
{{{dataType}}} {{paramName}} = routingContext.fileUploads().iterator().next();
|
||||
{{{dataType}}} {{paramName}} = routingContext.fileUploads().iterator().next();
|
||||
{{/isFile}}
|
||||
{{/isListContainer}}
|
||||
{{#isListContainer}}
|
||||
{{{dataType}}} {{paramName}} = routingContext.request().params().getAll("{{baseName}}");
|
||||
{{/isListContainer}}
|
||||
{{/isFormParam}}
|
||||
{{/isFormParam}}
|
||||
|
@ -1,13 +1,13 @@
|
||||
{{#isHeaderParam}}
|
||||
{{^isListContainer}}
|
||||
{{#isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.toObject(routingContext.request().getHeader("{{baseName}}"), {{dataType}}.class);
|
||||
{{{dataType}}} {{paramName}} = requestParameters.headerParameter("{{baseName}}") != null ? DatabindCodec.mapper().convertValue(requestParameters.headerParameter("{{baseName}}").get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isModel}}
|
||||
{{^isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.to{{dataType}}(routingContext.request().getHeader("{{baseName}}"));
|
||||
{{{dataType}}} {{paramName}} = requestParameters.headerParameter("{{baseName}}") != null ? requestParameters.headerParameter("{{baseName}}").get{{dataType}}() : null;
|
||||
{{/isModel}}
|
||||
{{/isListContainer}}
|
||||
{{#isListContainer}}
|
||||
{{{dataType}}} {{paramName}} = routingContext.request().params().getAll("{{baseName}}");
|
||||
{{{dataType}}} {{paramName}} = requestParameters.headerParameter("{{baseName}}") != null ? DatabindCodec.mapper().convertValue(requestParameters.headerParameter("{{baseName}}").get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isListContainer}}
|
||||
{{/isHeaderParam}}
|
||||
{{/isHeaderParam}}
|
||||
|
@ -1,13 +1,13 @@
|
||||
{{#isPathParam}}
|
||||
{{^isListContainer}}
|
||||
{{#isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.toObject(routingContext.pathParams().get("{{baseName}}"), {{dataType}}.class);
|
||||
{{{dataType}}} {{paramName}} = requestParameters.pathParameter("{{baseName}}") != null ? DatabindCodec.mapper().convertValue(requestParameters.pathParameter("{{baseName}}").get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isModel}}
|
||||
{{^isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.to{{dataType}}(routingContext.pathParams().get("{{baseName}}"));
|
||||
{{{dataType}}} {{paramName}} = requestParameters.pathParameter("{{baseName}}") != null ? requestParameters.pathParameter("{{baseName}}").get{{dataType}}() : null;
|
||||
{{/isModel}}
|
||||
{{/isListContainer}}
|
||||
{{#isListContainer}}
|
||||
{{{dataType}}} {{paramName}} = routingContext.request().params().getAll("{{baseName}}");
|
||||
{{{dataType}}} {{paramName}} = requestParameters.pathParameter("{{baseName}}") != null ? DatabindCodec.mapper().convertValue(requestParameters.pathParameter("{{baseName}}").get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isListContainer}}
|
||||
{{/isPathParam}}
|
||||
{{/isPathParam}}
|
||||
|
@ -1,13 +1,13 @@
|
||||
{{#isQueryParam}}
|
||||
{{^isListContainer}}
|
||||
{{#isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.toObject(routingContext.queryParams().get("{{baseName}}"), {{dataType}}.class);
|
||||
{{{dataType}}} {{paramName}} = requestParameters.queryParameter("{{baseName}}") != null ? DatabindCodec.mapper().convertValue(requestParameters.queryParameter("{{baseName}}").get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isModel}}
|
||||
{{^isModel}}
|
||||
{{{dataType}}} {{paramName}} = ParameterCast.to{{dataType}}(routingContext.queryParams().get("{{baseName}}"));
|
||||
{{{dataType}}} {{paramName}} = requestParameters.queryParameter("{{baseName}}") != null ? requestParameters.queryParameter("{{baseName}}").get{{dataType}}() : null;
|
||||
{{/isModel}}
|
||||
{{/isListContainer}}
|
||||
{{#isListContainer}}
|
||||
{{{dataType}}} {{paramName}} = routingContext.request().params().getAll("{{baseName}}");
|
||||
{{{dataType}}} {{paramName}} = requestParameters.queryParameter("{{baseName}}") != null ? DatabindCodec.mapper().convertValue(requestParameters.queryParameter("{{baseName}}").get(), new TypeReference<{{{dataType}}}>(){}) : null;
|
||||
{{/isListContainer}}
|
||||
{{/isQueryParam}}
|
||||
{{/isQueryParam}}
|
||||
|
@ -1,24 +0,0 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
public class ApiException extends RuntimeException {
|
||||
private final String message;
|
||||
private Integer statusCode;
|
||||
|
||||
public ApiException(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public ApiException setStatusCode(Integer statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
}
|
@ -2,28 +2,36 @@ package {{invokerPackage}};
|
||||
|
||||
public class ApiResponse<T> {
|
||||
private final T data;
|
||||
private Integer statusCode;
|
||||
private final int statusCode;
|
||||
|
||||
public ApiResponse() {
|
||||
this(200, null);
|
||||
}
|
||||
|
||||
public ApiResponse(T data) {
|
||||
this(200, data);
|
||||
}
|
||||
|
||||
public ApiResponse(int statusCode) {
|
||||
this(statusCode, null);
|
||||
}
|
||||
|
||||
public ApiResponse(int statusCode, T data) {
|
||||
this.statusCode = statusCode;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ApiResponse() {
|
||||
this.data = null;
|
||||
public boolean hasData() {
|
||||
return data != null;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ApiResponse<T> setStatusCode(Integer statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatusCode() {
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -1,66 +1,54 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.api.contract.RouterFactoryOptions;
|
||||
import io.vertx.reactivex.core.AbstractVerticle;
|
||||
import io.vertx.reactivex.ext.web.api.contract.openapi3.OpenAPI3RouterFactory;
|
||||
import io.vertx.ext.web.openapi.RouterFactory;
|
||||
import io.vertx.ext.web.openapi.RouterFactoryOptions;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
{{#apiInfo}}{{#apis}}
|
||||
import {{apiPackage}}.{{classname}}Handler;{{/apis}}{{/apiInfo}}
|
||||
|
||||
public class HttpServerVerticle extends AbstractVerticle {
|
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HttpServerVerticle.class);
|
||||
private static final String specFile = "src/main/resources/openapi.yaml";
|
||||
|
||||
private static String specFile = "src/main/resources/openapi.yaml";
|
||||
|
||||
private Map<String, Handler<RoutingContext>> operationHandlers = new HashMap<>();
|
||||
{{#apiInfo}}{{#apis}}
|
||||
private final {{classname}}Handler {{classVarName}}Handler = new {{classname}}Handler();{{/apis}}{{/apiInfo}}
|
||||
|
||||
@Override
|
||||
public void start(Future<Void> fut) {
|
||||
{{#apiInfo}}{{#apis}}
|
||||
new {{classname}}Handler(operationHandlers);{{/apis}}{{/apiInfo}}
|
||||
public void start(Promise<Void> startPromise) {
|
||||
RouterFactory.create(vertx, specFile)
|
||||
.map(factory -> {
|
||||
factory.setOptions(new RouterFactoryOptions()
|
||||
// For production use case, you need to enable this flag and provide the proper security handler
|
||||
.setRequireSecurityHandlers(false)
|
||||
);
|
||||
{{#apiInfo}}{{#apis}}
|
||||
{{classVarName}}Handler.mount(factory);{{/apis}}{{/apiInfo}}
|
||||
|
||||
OpenAPI3RouterFactory.rxCreate(vertx, specFile)
|
||||
.doOnSuccess(factory -> {
|
||||
factory.setOptions(new RouterFactoryOptions()
|
||||
.setRequireSecurityHandlers(false)
|
||||
.setMountNotImplementedHandler(false));
|
||||
factory.setValidationFailureHandler(this::validationFailureHandler);
|
||||
operationHandlers.forEach(factory.getDelegate()::addHandlerByOperationId);
|
||||
})
|
||||
.map(OpenAPI3RouterFactory::getRouter)
|
||||
.doOnSuccess(router -> router.route().last().handler(this::methodNotFoundHandler))
|
||||
.map(router ->
|
||||
vertx.createHttpServer(new HttpServerOptions()
|
||||
.setPort(8080)
|
||||
.setHost("localhost"))
|
||||
.requestHandler(router)
|
||||
.listen()
|
||||
)
|
||||
.subscribe( httpServer -> {
|
||||
logger.info("Http verticle deployed successful");
|
||||
fut.complete();
|
||||
}, error -> {
|
||||
logger.info("Http verticle failed to deployed", error);
|
||||
fut.fail(error.getCause());
|
||||
});
|
||||
Router router = factory.createRouter();
|
||||
router.errorHandler(400, this::validationFailureHandler);
|
||||
|
||||
return router;
|
||||
})
|
||||
.compose(router ->
|
||||
vertx.createHttpServer()
|
||||
.requestHandler(router)
|
||||
.listen(8080)
|
||||
)
|
||||
.onSuccess(server -> logger.info("Http verticle deploy successful"))
|
||||
.onFailure(t -> logger.error("Http verticle failed to deploy", t))
|
||||
// Complete the start promise
|
||||
.<Void>mapEmpty().onComplete(startPromise);
|
||||
}
|
||||
|
||||
private void validationFailureHandler(io.vertx.reactivex.ext.web.RoutingContext rc) {
|
||||
private void validationFailureHandler(RoutingContext rc) {
|
||||
rc.response().setStatusCode(400)
|
||||
.end("Bad Request : " + rc.failure().getMessage());
|
||||
}
|
||||
|
||||
private void methodNotFoundHandler(io.vertx.reactivex.ext.web.RoutingContext rc) {
|
||||
rc.response().setStatusCode(404)
|
||||
.end("Method '" + rc.request().path() + "' Not Found");
|
||||
}
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
import io.vertx.core.DeploymentOptions;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.reactivex.core.AbstractVerticle;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MainVerticle extends AbstractVerticle {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MainVerticle.class);
|
||||
|
||||
@Override
|
||||
public void start(Future<Void> future) {
|
||||
|
||||
DeploymentOptions options = new DeploymentOptions();
|
||||
options.setInstances(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
vertx.deployVerticle(HttpServerVerticle.class.getName(), options, res -> {
|
||||
if (!res.succeeded()) {
|
||||
logger.error("Deployment fail reason: ", res.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,88 +0,0 @@
|
||||
package {{invokerPackage}};
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.Json;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class ParameterCast {
|
||||
|
||||
public static Boolean toBoolean(String str) {
|
||||
if (str != null) {
|
||||
return Boolean.parseBoolean(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static UUID toUUID(String str) {
|
||||
if (str != null) {
|
||||
return UUID.fromString(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer toInteger(String str) {
|
||||
if (str != null) {
|
||||
return Integer.parseInt(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Long toLong(String str) {
|
||||
if (str != null) {
|
||||
return Long.parseLong(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Double toDouble(String str) {
|
||||
if (str != null){
|
||||
return Double.parseDouble(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Float toFloat(String str) {
|
||||
if (str != null){
|
||||
return Float.parseFloat(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static OffsetDateTime toOffsetDateTime(String str) {
|
||||
if (str != null) {
|
||||
return OffsetDateTime.parse(str,
|
||||
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toString(String str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
public static <T> T toObject(String str) {
|
||||
if (str != null) {
|
||||
return Json.decodeValue(str, new TypeReference<T>(){});
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T toObject(String str, Class<T> classz) {
|
||||
if (str != null) {
|
||||
return Json.decodeValue(str, classz);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,62 +1,77 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<groupId>{{groupId}}</groupId>
|
||||
<artifactId>{{artifactId}}</artifactId>
|
||||
<version>{{artifactVersion}}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>{{appName}}</name>
|
||||
|
||||
|
||||
<name>{{appName}}</name>
|
||||
|
||||
<properties>
|
||||
<vertx.version>3.6.3</vertx.version>
|
||||
<slf4j.version>1.7.26</slf4j.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||
|
||||
<vertx.version>4.0.0.Beta3</vertx.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<slf4j.version>1.7.30</slf4j.version>
|
||||
<jackson.version>2.11.2</jackson.version>
|
||||
|
||||
<main.verticle>{{invokerPackage}}.HttpServerVerticle</main.verticle>
|
||||
</properties>
|
||||
|
||||
<!-- Vert.x BOM -->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-stack-depchain</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web-openapi</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web-api-contract</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-rx-java2</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.reactivex.rxjava2</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test deps -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-junit5</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-unit</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@ -65,16 +80,15 @@
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
@ -84,19 +98,41 @@
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<manifestEntries>
|
||||
<Main-Class>io.vertx.core.Starter</Main-Class>
|
||||
<Main-Verticle>{{invokerPackage}}.MainVerticle</Main-Verticle>
|
||||
<Main-Class>io.vertx.core.Launcher</Main-Class>
|
||||
<Main-Verticle>${main.verticle}</Main-Verticle>
|
||||
</manifestEntries>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<artifactSet/>
|
||||
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
|
||||
<artifactSet>
|
||||
</artifactSet>
|
||||
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar
|
||||
</outputFile>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<mainClass>io.vertx.core.Launcher</mainClass>
|
||||
<arguments>
|
||||
<argument>run</argument>
|
||||
<argument>${main.verticle}</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
</project>
|
||||
|
2
pom.xml
2
pom.xml
@ -1291,7 +1291,7 @@
|
||||
<module>samples/server/petstore/scala-play-server</module>
|
||||
<module>samples/server/petstore/scala-akka-http-server</module>
|
||||
<module>samples/server/petstore/scalatra</module>
|
||||
<module>samples/server/petstore/java-vertx-web/rx</module>
|
||||
<module>samples/server/petstore/java-vertx-web</module>
|
||||
<module>samples/server/petstore/scala-finch</module>
|
||||
<!--<module>samples/server/petstore/kotlin/vertx</module>-->
|
||||
<module>samples/server/petstore/kotlin-springboot</module>
|
||||
|
@ -1,10 +1,7 @@
|
||||
README.md
|
||||
pom.xml
|
||||
src/main/java/org/openapitools/vertxweb/server/ApiException.java
|
||||
src/main/java/org/openapitools/vertxweb/server/ApiResponse.java
|
||||
src/main/java/org/openapitools/vertxweb/server/HttpServerVerticle.java
|
||||
src/main/java/org/openapitools/vertxweb/server/MainVerticle.java
|
||||
src/main/java/org/openapitools/vertxweb/server/ParameterCast.java
|
||||
src/main/java/org/openapitools/vertxweb/server/api/PetApi.java
|
||||
src/main/java/org/openapitools/vertxweb/server/api/PetApiHandler.java
|
||||
src/main/java/org/openapitools/vertxweb/server/api/PetApiImpl.java
|
@ -4,8 +4,15 @@
|
||||
This document assumes you have maven available.
|
||||
|
||||
To build the project using maven, run:
|
||||
|
||||
```bash
|
||||
mvn package && java -jar target/target/java-vertx-web-rx-server-1.0.0-SNAPSHOT-fat.jar
|
||||
mvn package
|
||||
```
|
||||
|
||||
To run the project, run the jar or use maven exec plugin:
|
||||
|
||||
```bash
|
||||
mvn exec:java
|
||||
```
|
||||
|
||||
If all builds successfully, the server should run on [http://localhost:8080/](http://localhost:8080/)
|
138
samples/server/petstore/java-vertx-web/pom.xml
Normal file
138
samples/server/petstore/java-vertx-web/pom.xml
Normal file
@ -0,0 +1,138 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.openapitools</groupId>
|
||||
<artifactId>java-vertx-web-server</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>OpenAPI Petstore</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
|
||||
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
|
||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||
|
||||
<vertx.version>4.0.0.Beta3</vertx.version>
|
||||
<junit-jupiter.version>5.6.2</junit-jupiter.version>
|
||||
<slf4j.version>1.7.30</slf4j.version>
|
||||
<jackson.version>2.11.2</jackson.version>
|
||||
|
||||
<main.verticle>org.openapitools.vertxweb.server.HttpServerVerticle</main.verticle>
|
||||
</properties>
|
||||
|
||||
<!-- Vert.x BOM -->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-stack-depchain</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web-openapi</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Test deps -->
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-junit5</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>${maven-shade-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<manifestEntries>
|
||||
<Main-Class>io.vertx.core.Launcher</Main-Class>
|
||||
<Main-Verticle>${main.verticle}</Main-Verticle>
|
||||
</manifestEntries>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/services/io.vertx.core.spi.VerticleFactory</resource>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<artifactSet>
|
||||
</artifactSet>
|
||||
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar
|
||||
</outputFile>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<mainClass>io.vertx.core.Launcher</mainClass>
|
||||
<arguments>
|
||||
<argument>run</argument>
|
||||
<argument>${main.verticle}</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,102 +0,0 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.openapitools</groupId>
|
||||
<artifactId>java-vertx-web-rx-server</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>OpenAPI Petstore</name>
|
||||
|
||||
<properties>
|
||||
<vertx.version>3.6.3</vertx.version>
|
||||
<slf4j.version>1.7.26</slf4j.version>
|
||||
<junit.version>4.13</junit.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-core</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-web-api-contract</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-rx-java2</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.reactivex.rxjava2</groupId>
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>2.2.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.vertx</groupId>
|
||||
<artifactId>vertx-unit</artifactId>
|
||||
<version>${vertx.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.3</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<manifestEntries>
|
||||
<Main-Class>io.vertx.core.Starter</Main-Class>
|
||||
<Main-Verticle>org.openapitools.vertxweb.server.MainVerticle</Main-Verticle>
|
||||
</manifestEntries>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<artifactSet/>
|
||||
<outputFile>${project.build.directory}/${project.artifactId}-${project.version}-fat.jar</outputFile>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -1,24 +0,0 @@
|
||||
package org.openapitools.vertxweb.server;
|
||||
|
||||
public class ApiException extends RuntimeException {
|
||||
private final String message;
|
||||
private Integer statusCode;
|
||||
|
||||
public ApiException(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public ApiException setStatusCode(Integer statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
package org.openapitools.vertxweb.server;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.api.contract.RouterFactoryOptions;
|
||||
import io.vertx.reactivex.core.AbstractVerticle;
|
||||
import io.vertx.reactivex.ext.web.api.contract.openapi3.OpenAPI3RouterFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.openapitools.vertxweb.server.api.PetApiHandler;
|
||||
import org.openapitools.vertxweb.server.api.StoreApiHandler;
|
||||
import org.openapitools.vertxweb.server.api.UserApiHandler;
|
||||
|
||||
public class HttpServerVerticle extends AbstractVerticle {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HttpServerVerticle.class);
|
||||
|
||||
private static String specFile = "src/main/resources/openapi.yaml";
|
||||
|
||||
private Map<String, Handler<RoutingContext>> operationHandlers = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void start(Future<Void> fut) {
|
||||
|
||||
new PetApiHandler(operationHandlers);
|
||||
new StoreApiHandler(operationHandlers);
|
||||
new UserApiHandler(operationHandlers);
|
||||
|
||||
OpenAPI3RouterFactory.rxCreate(vertx, specFile)
|
||||
.doOnSuccess(factory -> {
|
||||
factory.setOptions(new RouterFactoryOptions()
|
||||
.setRequireSecurityHandlers(false)
|
||||
.setMountNotImplementedHandler(false));
|
||||
factory.setValidationFailureHandler(this::validationFailureHandler);
|
||||
operationHandlers.forEach(factory.getDelegate()::addHandlerByOperationId);
|
||||
})
|
||||
.map(OpenAPI3RouterFactory::getRouter)
|
||||
.doOnSuccess(router -> router.route().last().handler(this::methodNotFoundHandler))
|
||||
.map(router ->
|
||||
vertx.createHttpServer(new HttpServerOptions()
|
||||
.setPort(8080)
|
||||
.setHost("localhost"))
|
||||
.requestHandler(router)
|
||||
.listen()
|
||||
)
|
||||
.subscribe( httpServer -> {
|
||||
logger.info("Http verticle deployed successful");
|
||||
fut.complete();
|
||||
}, error -> {
|
||||
logger.info("Http verticle failed to deployed", error);
|
||||
fut.fail(error.getCause());
|
||||
});
|
||||
}
|
||||
|
||||
private void validationFailureHandler(io.vertx.reactivex.ext.web.RoutingContext rc) {
|
||||
rc.response().setStatusCode(400)
|
||||
.end("Bad Request : " + rc.failure().getMessage());
|
||||
}
|
||||
|
||||
private void methodNotFoundHandler(io.vertx.reactivex.ext.web.RoutingContext rc) {
|
||||
rc.response().setStatusCode(404)
|
||||
.end("Method '" + rc.request().path() + "' Not Found");
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package org.openapitools.vertxweb.server;
|
||||
|
||||
import io.vertx.core.DeploymentOptions;
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.reactivex.core.AbstractVerticle;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MainVerticle extends AbstractVerticle {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MainVerticle.class);
|
||||
|
||||
@Override
|
||||
public void start(Future<Void> future) {
|
||||
|
||||
DeploymentOptions options = new DeploymentOptions();
|
||||
options.setInstances(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
vertx.deployVerticle(HttpServerVerticle.class.getName(), options, res -> {
|
||||
if (!res.succeeded()) {
|
||||
logger.error("Deployment fail reason: ", res.cause());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,88 +0,0 @@
|
||||
package org.openapitools.vertxweb.server;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.Json;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class ParameterCast {
|
||||
|
||||
public static Boolean toBoolean(String str) {
|
||||
if (str != null) {
|
||||
return Boolean.parseBoolean(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static UUID toUUID(String str) {
|
||||
if (str != null) {
|
||||
return UUID.fromString(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer toInteger(String str) {
|
||||
if (str != null) {
|
||||
return Integer.parseInt(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Long toLong(String str) {
|
||||
if (str != null) {
|
||||
return Long.parseLong(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Double toDouble(String str) {
|
||||
if (str != null){
|
||||
return Double.parseDouble(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Float toFloat(String str) {
|
||||
if (str != null){
|
||||
return Float.parseFloat(str);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static OffsetDateTime toOffsetDateTime(String str) {
|
||||
if (str != null) {
|
||||
return OffsetDateTime.parse(str,
|
||||
DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String toString(String str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
public static <T> T toObject(String str) {
|
||||
if (str != null) {
|
||||
return Json.decodeValue(str, new TypeReference<T>(){});
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T toObject(String str, Class<T> classz) {
|
||||
if (str != null) {
|
||||
return Json.decodeValue(str, classz);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import io.vertx.ext.web.FileUpload;
|
||||
import org.openapitools.vertxweb.server.model.ModelApiResponse;
|
||||
import org.openapitools.vertxweb.server.model.Pet;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface PetApi {
|
||||
Single<ApiResponse<Pet>> addPet(Pet pet);
|
||||
Single<ApiResponse<Void>> deletePet(Long petId,String apiKey);
|
||||
Single<ApiResponse<List<Pet>>> findPetsByStatus(List<String> status);
|
||||
Single<ApiResponse<List<Pet>>> findPetsByTags(List<String> tags);
|
||||
Single<ApiResponse<Pet>> getPetById(Long petId);
|
||||
Single<ApiResponse<Pet>> updatePet(Pet pet);
|
||||
Single<ApiResponse<Void>> updatePetWithForm(Long petId,String name,String status);
|
||||
Single<ApiResponse<ModelApiResponse>> uploadFile(Long petId,String additionalMetadata,FileUpload file);
|
||||
}
|
@ -1,250 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import io.vertx.ext.web.FileUpload;
|
||||
import org.openapitools.vertxweb.server.model.ModelApiResponse;
|
||||
import org.openapitools.vertxweb.server.model.Pet;
|
||||
|
||||
import org.openapitools.vertxweb.server.ParameterCast;
|
||||
import org.openapitools.vertxweb.server.ApiException;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PetApiHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PetApiHandler.class);
|
||||
private PetApi apiImpl = new PetApiImpl();
|
||||
|
||||
public PetApiHandler(Map<String, Handler<RoutingContext>> operationHandlers) {
|
||||
operationHandlers.put("addPet", this::addPet);
|
||||
operationHandlers.put("deletePet", this::deletePet);
|
||||
operationHandlers.put("findPetsByStatus", this::findPetsByStatus);
|
||||
operationHandlers.put("findPetsByTags", this::findPetsByTags);
|
||||
operationHandlers.put("getPetById", this::getPetById);
|
||||
operationHandlers.put("updatePet", this::updatePet);
|
||||
operationHandlers.put("updatePetWithForm", this::updatePetWithForm);
|
||||
operationHandlers.put("uploadFile", this::uploadFile);
|
||||
}
|
||||
|
||||
private void addPet(RoutingContext routingContext) {
|
||||
logger.info("addPet()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
Pet pet = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<Pet>(){});
|
||||
logger.info("Parameter pet is {}", pet);
|
||||
return apiImpl.addPet(pet);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void deletePet(RoutingContext routingContext) {
|
||||
logger.info("deletePet()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
Long petId = ParameterCast.toLong(routingContext.pathParams().get("petId"));
|
||||
String apiKey = ParameterCast.toString(routingContext.request().getHeader("api_key"));
|
||||
|
||||
logger.info("Parameter petId is {}", petId);
|
||||
logger.info("Parameter apiKey is {}", apiKey);
|
||||
return apiImpl.deletePet(petId, apiKey);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void findPetsByStatus(RoutingContext routingContext) {
|
||||
logger.info("findPetsByStatus()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
List<String> status = routingContext.request().params().getAll("status");
|
||||
|
||||
logger.info("Parameter status is {}", status);
|
||||
return apiImpl.findPetsByStatus(status);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void findPetsByTags(RoutingContext routingContext) {
|
||||
logger.info("findPetsByTags()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
List<String> tags = routingContext.request().params().getAll("tags");
|
||||
|
||||
logger.info("Parameter tags is {}", tags);
|
||||
return apiImpl.findPetsByTags(tags);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void getPetById(RoutingContext routingContext) {
|
||||
logger.info("getPetById()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
Long petId = ParameterCast.toLong(routingContext.pathParams().get("petId"));
|
||||
|
||||
logger.info("Parameter petId is {}", petId);
|
||||
return apiImpl.getPetById(petId);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void updatePet(RoutingContext routingContext) {
|
||||
logger.info("updatePet()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
Pet pet = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<Pet>(){});
|
||||
logger.info("Parameter pet is {}", pet);
|
||||
return apiImpl.updatePet(pet);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void updatePetWithForm(RoutingContext routingContext) {
|
||||
logger.info("updatePetWithForm()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
Long petId = ParameterCast.toLong(routingContext.pathParams().get("petId"));
|
||||
String name = ParameterCast.toString(routingContext.request().getFormAttribute("name"));
|
||||
String status = ParameterCast.toString(routingContext.request().getFormAttribute("status"));
|
||||
|
||||
logger.info("Parameter petId is {}", petId);
|
||||
logger.info("Parameter name is {}", name);
|
||||
logger.info("Parameter status is {}", status);
|
||||
return apiImpl.updatePetWithForm(petId, name, status);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void uploadFile(RoutingContext routingContext) {
|
||||
logger.info("uploadFile()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
Long petId = ParameterCast.toLong(routingContext.pathParams().get("petId"));
|
||||
String additionalMetadata = ParameterCast.toString(routingContext.request().getFormAttribute("additionalMetadata"));
|
||||
FileUpload file = routingContext.fileUploads().iterator().next();
|
||||
|
||||
logger.info("Parameter petId is {}", petId);
|
||||
logger.info("Parameter additionalMetadata is {}", additionalMetadata);
|
||||
logger.info("Parameter file is {}", file);
|
||||
return apiImpl.uploadFile(petId, additionalMetadata, file);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import io.vertx.ext.web.FileUpload;
|
||||
import org.openapitools.vertxweb.server.model.ModelApiResponse;
|
||||
import org.openapitools.vertxweb.server.model.Pet;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
import org.openapitools.vertxweb.server.ApiException;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// Implement this class
|
||||
|
||||
public class PetApiImpl implements PetApi {
|
||||
public Single<ApiResponse<Pet>> addPet(Pet pet) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> deletePet(Long petId,String apiKey) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<List<Pet>>> findPetsByStatus(List<String> status) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<List<Pet>>> findPetsByTags(List<String> tags) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Pet>> getPetById(Long petId) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Pet>> updatePet(Pet pet) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> updatePetWithForm(Long petId,String name,String status) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<ModelApiResponse>> uploadFile(Long petId,String additionalMetadata,FileUpload file) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.Order;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface StoreApi {
|
||||
Single<ApiResponse<Void>> deleteOrder(String orderId);
|
||||
Single<ApiResponse<Map<String, Integer>>> getInventory();
|
||||
Single<ApiResponse<Order>> getOrderById(Long orderId);
|
||||
Single<ApiResponse<Order>> placeOrder(Order order);
|
||||
}
|
@ -1,131 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.Order;
|
||||
|
||||
import org.openapitools.vertxweb.server.ParameterCast;
|
||||
import org.openapitools.vertxweb.server.ApiException;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StoreApiHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(StoreApiHandler.class);
|
||||
private StoreApi apiImpl = new StoreApiImpl();
|
||||
|
||||
public StoreApiHandler(Map<String, Handler<RoutingContext>> operationHandlers) {
|
||||
operationHandlers.put("deleteOrder", this::deleteOrder);
|
||||
operationHandlers.put("getInventory", this::getInventory);
|
||||
operationHandlers.put("getOrderById", this::getOrderById);
|
||||
operationHandlers.put("placeOrder", this::placeOrder);
|
||||
}
|
||||
|
||||
private void deleteOrder(RoutingContext routingContext) {
|
||||
logger.info("deleteOrder()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
String orderId = ParameterCast.toString(routingContext.pathParams().get("orderId"));
|
||||
|
||||
logger.info("Parameter orderId is {}", orderId);
|
||||
return apiImpl.deleteOrder(orderId);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void getInventory(RoutingContext routingContext) {
|
||||
logger.info("getInventory()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
return apiImpl.getInventory();
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void getOrderById(RoutingContext routingContext) {
|
||||
logger.info("getOrderById()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
Long orderId = ParameterCast.toLong(routingContext.pathParams().get("orderId"));
|
||||
|
||||
logger.info("Parameter orderId is {}", orderId);
|
||||
return apiImpl.getOrderById(orderId);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void placeOrder(RoutingContext routingContext) {
|
||||
logger.info("placeOrder()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
Order order = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<Order>(){});
|
||||
logger.info("Parameter order is {}", order);
|
||||
return apiImpl.placeOrder(order);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.Order;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
import org.openapitools.vertxweb.server.ApiException;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// Implement this class
|
||||
|
||||
public class StoreApiImpl implements StoreApi {
|
||||
public Single<ApiResponse<Void>> deleteOrder(String orderId) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Map<String, Integer>>> getInventory() {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Order>> getOrderById(Long orderId) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Order>> placeOrder(Order order) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.User;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface UserApi {
|
||||
Single<ApiResponse<Void>> createUser(User user);
|
||||
Single<ApiResponse<Void>> createUsersWithArrayInput(List<User> user);
|
||||
Single<ApiResponse<Void>> createUsersWithListInput(List<User> user);
|
||||
Single<ApiResponse<Void>> deleteUser(String username);
|
||||
Single<ApiResponse<User>> getUserByName(String username);
|
||||
Single<ApiResponse<String>> loginUser(String username,String password);
|
||||
Single<ApiResponse<Void>> logoutUser();
|
||||
Single<ApiResponse<Void>> updateUser(String username,User user);
|
||||
}
|
@ -1,242 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.User;
|
||||
|
||||
import org.openapitools.vertxweb.server.ParameterCast;
|
||||
import org.openapitools.vertxweb.server.ApiException;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.Json;
|
||||
import io.vertx.core.Handler;
|
||||
import io.vertx.core.http.HttpServerResponse;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class UserApiHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UserApiHandler.class);
|
||||
private UserApi apiImpl = new UserApiImpl();
|
||||
|
||||
public UserApiHandler(Map<String, Handler<RoutingContext>> operationHandlers) {
|
||||
operationHandlers.put("createUser", this::createUser);
|
||||
operationHandlers.put("createUsersWithArrayInput", this::createUsersWithArrayInput);
|
||||
operationHandlers.put("createUsersWithListInput", this::createUsersWithListInput);
|
||||
operationHandlers.put("deleteUser", this::deleteUser);
|
||||
operationHandlers.put("getUserByName", this::getUserByName);
|
||||
operationHandlers.put("loginUser", this::loginUser);
|
||||
operationHandlers.put("logoutUser", this::logoutUser);
|
||||
operationHandlers.put("updateUser", this::updateUser);
|
||||
}
|
||||
|
||||
private void createUser(RoutingContext routingContext) {
|
||||
logger.info("createUser()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
User user = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<User>(){});
|
||||
logger.info("Parameter user is {}", user);
|
||||
return apiImpl.createUser(user);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void createUsersWithArrayInput(RoutingContext routingContext) {
|
||||
logger.info("createUsersWithArrayInput()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
List<User> user = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<List<User>>(){});
|
||||
logger.info("Parameter user is {}", user);
|
||||
return apiImpl.createUsersWithArrayInput(user);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void createUsersWithListInput(RoutingContext routingContext) {
|
||||
logger.info("createUsersWithListInput()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
List<User> user = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<List<User>>(){});
|
||||
logger.info("Parameter user is {}", user);
|
||||
return apiImpl.createUsersWithListInput(user);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void deleteUser(RoutingContext routingContext) {
|
||||
logger.info("deleteUser()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
String username = ParameterCast.toString(routingContext.pathParams().get("username"));
|
||||
|
||||
logger.info("Parameter username is {}", username);
|
||||
return apiImpl.deleteUser(username);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void getUserByName(RoutingContext routingContext) {
|
||||
logger.info("getUserByName()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
String username = ParameterCast.toString(routingContext.pathParams().get("username"));
|
||||
|
||||
logger.info("Parameter username is {}", username);
|
||||
return apiImpl.getUserByName(username);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void loginUser(RoutingContext routingContext) {
|
||||
logger.info("loginUser()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
String username = ParameterCast.toString(routingContext.queryParams().get("username"));
|
||||
String password = ParameterCast.toString(routingContext.queryParams().get("password"));
|
||||
|
||||
logger.info("Parameter username is {}", username);
|
||||
logger.info("Parameter password is {}", password);
|
||||
return apiImpl.loginUser(username, password);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void logoutUser(RoutingContext routingContext) {
|
||||
logger.info("logoutUser()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
|
||||
return apiImpl.logoutUser();
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
|
||||
private void updateUser(RoutingContext routingContext) {
|
||||
logger.info("updateUser()");
|
||||
HttpServerResponse response = routingContext.response();
|
||||
|
||||
Single.defer( () -> {
|
||||
String username = ParameterCast.toString(routingContext.pathParams().get("username"));
|
||||
|
||||
String jsonString = routingContext.getBodyAsString();
|
||||
User user = jsonString == null ? null : Json.decodeValue(jsonString, new TypeReference<User>(){});
|
||||
logger.info("Parameter username is {}", username);
|
||||
logger.info("Parameter user is {}", user);
|
||||
return apiImpl.updateUser(username, user);
|
||||
})
|
||||
.subscribe(
|
||||
apiResponse -> {
|
||||
response.setStatusCode(apiResponse.getStatusCode())
|
||||
.end(Json.encodePrettily(apiResponse.getData()));
|
||||
}, error -> {
|
||||
if (error instanceof ApiException) {
|
||||
ApiException apiException = (ApiException) error;
|
||||
response.setStatusCode(apiException.getStatusCode()).end(apiException.getMessage());
|
||||
} else {
|
||||
response.setStatusCode(500).end(error.getMessage());
|
||||
}
|
||||
}).dispose();
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.User;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
import org.openapitools.vertxweb.server.ApiException;
|
||||
|
||||
import io.reactivex.Single;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// Implement this class
|
||||
|
||||
public class UserApiImpl implements UserApi {
|
||||
public Single<ApiResponse<Void>> createUser(User user) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> createUsersWithArrayInput(List<User> user) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> createUsersWithListInput(List<User> user) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> deleteUser(String username) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<User>> getUserByName(String username) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<String>> loginUser(String username,String password) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> logoutUser() {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
public Single<ApiResponse<Void>> updateUser(String username,User user) {
|
||||
return Single.error(new ApiException("Not Implemented").setStatusCode(501));
|
||||
}
|
||||
|
||||
}
|
@ -2,28 +2,36 @@ package org.openapitools.vertxweb.server;
|
||||
|
||||
public class ApiResponse<T> {
|
||||
private final T data;
|
||||
private Integer statusCode;
|
||||
private final int statusCode;
|
||||
|
||||
public ApiResponse() {
|
||||
this(200, null);
|
||||
}
|
||||
|
||||
public ApiResponse(T data) {
|
||||
this(200, data);
|
||||
}
|
||||
|
||||
public ApiResponse(int statusCode) {
|
||||
this(statusCode, null);
|
||||
}
|
||||
|
||||
public ApiResponse(int statusCode, T data) {
|
||||
this.statusCode = statusCode;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ApiResponse() {
|
||||
this.data = null;
|
||||
public boolean hasData() {
|
||||
return data != null;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public ApiResponse<T> setStatusCode(Integer statusCode) {
|
||||
this.statusCode = statusCode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatusCode() {
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -0,0 +1,60 @@
|
||||
package org.openapitools.vertxweb.server;
|
||||
|
||||
import io.vertx.core.AbstractVerticle;
|
||||
import io.vertx.core.Promise;
|
||||
import io.vertx.core.http.HttpServerOptions;
|
||||
import io.vertx.ext.web.Router;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.ext.web.openapi.RouterFactory;
|
||||
import io.vertx.ext.web.openapi.RouterFactoryOptions;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.openapitools.vertxweb.server.api.PetApiHandler;
|
||||
import org.openapitools.vertxweb.server.api.StoreApiHandler;
|
||||
import org.openapitools.vertxweb.server.api.UserApiHandler;
|
||||
|
||||
public class HttpServerVerticle extends AbstractVerticle {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(HttpServerVerticle.class);
|
||||
private static final String specFile = "src/main/resources/openapi.yaml";
|
||||
|
||||
|
||||
private final PetApiHandler petHandler = new PetApiHandler();
|
||||
private final StoreApiHandler storeHandler = new StoreApiHandler();
|
||||
private final UserApiHandler userHandler = new UserApiHandler();
|
||||
|
||||
@Override
|
||||
public void start(Promise<Void> startPromise) {
|
||||
RouterFactory.create(vertx, specFile)
|
||||
.map(factory -> {
|
||||
factory.setOptions(new RouterFactoryOptions()
|
||||
// For production use case, you need to enable this flag and provide the proper security handler
|
||||
.setRequireSecurityHandlers(false)
|
||||
);
|
||||
|
||||
petHandler.mount(factory);
|
||||
storeHandler.mount(factory);
|
||||
userHandler.mount(factory);
|
||||
|
||||
Router router = factory.createRouter();
|
||||
router.errorHandler(400, this::validationFailureHandler);
|
||||
|
||||
return router;
|
||||
})
|
||||
.compose(router ->
|
||||
vertx.createHttpServer()
|
||||
.requestHandler(router)
|
||||
.listen(8080)
|
||||
)
|
||||
.onSuccess(server -> logger.info("Http verticle deploy successful"))
|
||||
.onFailure(t -> logger.error("Http verticle failed to deploy", t))
|
||||
// Complete the start promise
|
||||
.<Void>mapEmpty().onComplete(startPromise);
|
||||
}
|
||||
|
||||
private void validationFailureHandler(RoutingContext rc) {
|
||||
rc.response().setStatusCode(400)
|
||||
.end("Bad Request : " + rc.failure().getMessage());
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import io.vertx.ext.web.FileUpload;
|
||||
import org.openapitools.vertxweb.server.model.ModelApiResponse;
|
||||
import org.openapitools.vertxweb.server.model.Pet;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface PetApi {
|
||||
Future<ApiResponse<Pet>> addPet(Pet pet);
|
||||
Future<ApiResponse<Void>> deletePet(Long petId, String apiKey);
|
||||
Future<ApiResponse<List<Pet>>> findPetsByStatus(List<String> status);
|
||||
Future<ApiResponse<List<Pet>>> findPetsByTags(List<String> tags);
|
||||
Future<ApiResponse<Pet>> getPetById(Long petId);
|
||||
Future<ApiResponse<Pet>> updatePet(Pet pet);
|
||||
Future<ApiResponse<Void>> updatePetWithForm(Long petId, JsonObject formBody);
|
||||
Future<ApiResponse<ModelApiResponse>> uploadFile(Long petId, FileUpload file);
|
||||
}
|
@ -0,0 +1,227 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import io.vertx.ext.web.FileUpload;
|
||||
import org.openapitools.vertxweb.server.model.ModelApiResponse;
|
||||
import org.openapitools.vertxweb.server.model.Pet;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.jackson.DatabindCodec;
|
||||
import io.vertx.ext.web.openapi.RouterFactory;
|
||||
import io.vertx.ext.web.validation.RequestParameters;
|
||||
import io.vertx.ext.web.validation.RequestParameter;
|
||||
import io.vertx.ext.web.validation.ValidationHandler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PetApiHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(PetApiHandler.class);
|
||||
|
||||
private final PetApi apiImpl;
|
||||
|
||||
public PetApiHandler() {
|
||||
this.apiImpl = new PetApiImpl();
|
||||
}
|
||||
|
||||
public void mount(RouterFactory factory) {
|
||||
factory.operation("addPet").handler(this::addPet);
|
||||
factory.operation("deletePet").handler(this::deletePet);
|
||||
factory.operation("findPetsByStatus").handler(this::findPetsByStatus);
|
||||
factory.operation("findPetsByTags").handler(this::findPetsByTags);
|
||||
factory.operation("getPetById").handler(this::getPetById);
|
||||
factory.operation("updatePet").handler(this::updatePet);
|
||||
factory.operation("updatePetWithForm").handler(this::updatePetWithForm);
|
||||
factory.operation("uploadFile").handler(this::uploadFile);
|
||||
}
|
||||
|
||||
private void addPet(RoutingContext routingContext) {
|
||||
logger.info("addPet()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
RequestParameter body = requestParameters.body();
|
||||
Pet pet = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<Pet>(){}) : null;
|
||||
|
||||
logger.debug("Parameter pet is {}", pet);
|
||||
|
||||
apiImpl.addPet(pet)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void deletePet(RoutingContext routingContext) {
|
||||
logger.info("deletePet()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
Long petId = requestParameters.pathParameter("petId") != null ? requestParameters.pathParameter("petId").getLong() : null;
|
||||
String apiKey = requestParameters.headerParameter("api_key") != null ? requestParameters.headerParameter("api_key").getString() : null;
|
||||
|
||||
logger.debug("Parameter petId is {}", petId);
|
||||
logger.debug("Parameter apiKey is {}", apiKey);
|
||||
|
||||
apiImpl.deletePet(petId, apiKey)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void findPetsByStatus(RoutingContext routingContext) {
|
||||
logger.info("findPetsByStatus()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
List<String> status = requestParameters.queryParameter("status") != null ? DatabindCodec.mapper().convertValue(requestParameters.queryParameter("status").get(), new TypeReference<List<String>>(){}) : null;
|
||||
|
||||
logger.debug("Parameter status is {}", status);
|
||||
|
||||
apiImpl.findPetsByStatus(status)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void findPetsByTags(RoutingContext routingContext) {
|
||||
logger.info("findPetsByTags()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
List<String> tags = requestParameters.queryParameter("tags") != null ? DatabindCodec.mapper().convertValue(requestParameters.queryParameter("tags").get(), new TypeReference<List<String>>(){}) : null;
|
||||
|
||||
logger.debug("Parameter tags is {}", tags);
|
||||
|
||||
apiImpl.findPetsByTags(tags)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void getPetById(RoutingContext routingContext) {
|
||||
logger.info("getPetById()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
Long petId = requestParameters.pathParameter("petId") != null ? requestParameters.pathParameter("petId").getLong() : null;
|
||||
|
||||
logger.debug("Parameter petId is {}", petId);
|
||||
|
||||
apiImpl.getPetById(petId)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void updatePet(RoutingContext routingContext) {
|
||||
logger.info("updatePet()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
RequestParameter body = requestParameters.body();
|
||||
Pet pet = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<Pet>(){}) : null;
|
||||
|
||||
logger.debug("Parameter pet is {}", pet);
|
||||
|
||||
apiImpl.updatePet(pet)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void updatePetWithForm(RoutingContext routingContext) {
|
||||
logger.info("updatePetWithForm()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
Long petId = requestParameters.pathParameter("petId") != null ? requestParameters.pathParameter("petId").getLong() : null;
|
||||
RequestParameter body = requestParameters.body();
|
||||
JsonObject formBody = body != null ? body.getJsonObject() : null;
|
||||
|
||||
logger.debug("Parameter petId is {}", petId);
|
||||
logger.debug("Parameter formBody is {}", formBody);
|
||||
|
||||
apiImpl.updatePetWithForm(petId, formBody)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void uploadFile(RoutingContext routingContext) {
|
||||
logger.info("uploadFile()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
Long petId = requestParameters.pathParameter("petId") != null ? requestParameters.pathParameter("petId").getLong() : null;
|
||||
FileUpload file = routingContext.fileUploads().iterator().next();
|
||||
|
||||
logger.debug("Parameter petId is {}", petId);
|
||||
logger.debug("Parameter file is {}", file);
|
||||
|
||||
apiImpl.uploadFile(petId, file)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import io.vertx.ext.web.FileUpload;
|
||||
import org.openapitools.vertxweb.server.model.ModelApiResponse;
|
||||
import org.openapitools.vertxweb.server.model.Pet;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.handler.impl.HttpStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// Implement this class
|
||||
|
||||
public class PetApiImpl implements PetApi {
|
||||
public Future<ApiResponse<Pet>> addPet(Pet pet) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> deletePet(Long petId, String apiKey) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<List<Pet>>> findPetsByStatus(List<String> status) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<List<Pet>>> findPetsByTags(List<String> tags) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Pet>> getPetById(Long petId) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Pet>> updatePet(Pet pet) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> updatePetWithForm(Long petId, JsonObject formBody) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<ModelApiResponse>> uploadFile(Long petId, FileUpload file) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.Order;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface StoreApi {
|
||||
Future<ApiResponse<Void>> deleteOrder(String orderId);
|
||||
Future<ApiResponse<Map<String, Integer>>> getInventory();
|
||||
Future<ApiResponse<Order>> getOrderById(Long orderId);
|
||||
Future<ApiResponse<Order>> placeOrder(Order order);
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.Order;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.jackson.DatabindCodec;
|
||||
import io.vertx.ext.web.openapi.RouterFactory;
|
||||
import io.vertx.ext.web.validation.RequestParameters;
|
||||
import io.vertx.ext.web.validation.RequestParameter;
|
||||
import io.vertx.ext.web.validation.ValidationHandler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class StoreApiHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(StoreApiHandler.class);
|
||||
|
||||
private final StoreApi apiImpl;
|
||||
|
||||
public StoreApiHandler() {
|
||||
this.apiImpl = new StoreApiImpl();
|
||||
}
|
||||
|
||||
public void mount(RouterFactory factory) {
|
||||
factory.operation("deleteOrder").handler(this::deleteOrder);
|
||||
factory.operation("getInventory").handler(this::getInventory);
|
||||
factory.operation("getOrderById").handler(this::getOrderById);
|
||||
factory.operation("placeOrder").handler(this::placeOrder);
|
||||
}
|
||||
|
||||
private void deleteOrder(RoutingContext routingContext) {
|
||||
logger.info("deleteOrder()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
String orderId = requestParameters.pathParameter("orderId") != null ? requestParameters.pathParameter("orderId").getString() : null;
|
||||
|
||||
logger.debug("Parameter orderId is {}", orderId);
|
||||
|
||||
apiImpl.deleteOrder(orderId)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void getInventory(RoutingContext routingContext) {
|
||||
logger.info("getInventory()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
|
||||
|
||||
apiImpl.getInventory()
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void getOrderById(RoutingContext routingContext) {
|
||||
logger.info("getOrderById()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
Long orderId = requestParameters.pathParameter("orderId") != null ? requestParameters.pathParameter("orderId").getLong() : null;
|
||||
|
||||
logger.debug("Parameter orderId is {}", orderId);
|
||||
|
||||
apiImpl.getOrderById(orderId)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void placeOrder(RoutingContext routingContext) {
|
||||
logger.info("placeOrder()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
RequestParameter body = requestParameters.body();
|
||||
Order order = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<Order>(){}) : null;
|
||||
|
||||
logger.debug("Parameter order is {}", order);
|
||||
|
||||
apiImpl.placeOrder(order)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.Order;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.handler.impl.HttpStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// Implement this class
|
||||
|
||||
public class StoreApiImpl implements StoreApi {
|
||||
public Future<ApiResponse<Void>> deleteOrder(String orderId) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Map<String, Integer>>> getInventory() {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Order>> getOrderById(Long orderId) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Order>> placeOrder(Order order) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.User;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface UserApi {
|
||||
Future<ApiResponse<Void>> createUser(User user);
|
||||
Future<ApiResponse<Void>> createUsersWithArrayInput(List<User> user);
|
||||
Future<ApiResponse<Void>> createUsersWithListInput(List<User> user);
|
||||
Future<ApiResponse<Void>> deleteUser(String username);
|
||||
Future<ApiResponse<User>> getUserByName(String username);
|
||||
Future<ApiResponse<String>> loginUser(String username, String password);
|
||||
Future<ApiResponse<Void>> logoutUser();
|
||||
Future<ApiResponse<Void>> updateUser(String username, User user);
|
||||
}
|
@ -0,0 +1,222 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.User;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.vertx.core.json.jackson.DatabindCodec;
|
||||
import io.vertx.ext.web.openapi.RouterFactory;
|
||||
import io.vertx.ext.web.validation.RequestParameters;
|
||||
import io.vertx.ext.web.validation.RequestParameter;
|
||||
import io.vertx.ext.web.validation.ValidationHandler;
|
||||
import io.vertx.ext.web.RoutingContext;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class UserApiHandler {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(UserApiHandler.class);
|
||||
|
||||
private final UserApi apiImpl;
|
||||
|
||||
public UserApiHandler() {
|
||||
this.apiImpl = new UserApiImpl();
|
||||
}
|
||||
|
||||
public void mount(RouterFactory factory) {
|
||||
factory.operation("createUser").handler(this::createUser);
|
||||
factory.operation("createUsersWithArrayInput").handler(this::createUsersWithArrayInput);
|
||||
factory.operation("createUsersWithListInput").handler(this::createUsersWithListInput);
|
||||
factory.operation("deleteUser").handler(this::deleteUser);
|
||||
factory.operation("getUserByName").handler(this::getUserByName);
|
||||
factory.operation("loginUser").handler(this::loginUser);
|
||||
factory.operation("logoutUser").handler(this::logoutUser);
|
||||
factory.operation("updateUser").handler(this::updateUser);
|
||||
}
|
||||
|
||||
private void createUser(RoutingContext routingContext) {
|
||||
logger.info("createUser()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
RequestParameter body = requestParameters.body();
|
||||
User user = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<User>(){}) : null;
|
||||
|
||||
logger.debug("Parameter user is {}", user);
|
||||
|
||||
apiImpl.createUser(user)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void createUsersWithArrayInput(RoutingContext routingContext) {
|
||||
logger.info("createUsersWithArrayInput()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
RequestParameter body = requestParameters.body();
|
||||
List<User> user = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<List<User>>(){}) : null;
|
||||
|
||||
logger.debug("Parameter user is {}", user);
|
||||
|
||||
apiImpl.createUsersWithArrayInput(user)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void createUsersWithListInput(RoutingContext routingContext) {
|
||||
logger.info("createUsersWithListInput()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
RequestParameter body = requestParameters.body();
|
||||
List<User> user = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<List<User>>(){}) : null;
|
||||
|
||||
logger.debug("Parameter user is {}", user);
|
||||
|
||||
apiImpl.createUsersWithListInput(user)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void deleteUser(RoutingContext routingContext) {
|
||||
logger.info("deleteUser()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
String username = requestParameters.pathParameter("username") != null ? requestParameters.pathParameter("username").getString() : null;
|
||||
|
||||
logger.debug("Parameter username is {}", username);
|
||||
|
||||
apiImpl.deleteUser(username)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void getUserByName(RoutingContext routingContext) {
|
||||
logger.info("getUserByName()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
String username = requestParameters.pathParameter("username") != null ? requestParameters.pathParameter("username").getString() : null;
|
||||
|
||||
logger.debug("Parameter username is {}", username);
|
||||
|
||||
apiImpl.getUserByName(username)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void loginUser(RoutingContext routingContext) {
|
||||
logger.info("loginUser()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
String username = requestParameters.queryParameter("username") != null ? requestParameters.queryParameter("username").getString() : null;
|
||||
String password = requestParameters.queryParameter("password") != null ? requestParameters.queryParameter("password").getString() : null;
|
||||
|
||||
logger.debug("Parameter username is {}", username);
|
||||
logger.debug("Parameter password is {}", password);
|
||||
|
||||
apiImpl.loginUser(username, password)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void logoutUser(RoutingContext routingContext) {
|
||||
logger.info("logoutUser()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
|
||||
|
||||
apiImpl.logoutUser()
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
private void updateUser(RoutingContext routingContext) {
|
||||
logger.info("updateUser()");
|
||||
|
||||
// Param extraction
|
||||
RequestParameters requestParameters = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
|
||||
|
||||
String username = requestParameters.pathParameter("username") != null ? requestParameters.pathParameter("username").getString() : null;
|
||||
RequestParameter body = requestParameters.body();
|
||||
User user = body != null ? DatabindCodec.mapper().convertValue(body.get(), new TypeReference<User>(){}) : null;
|
||||
|
||||
logger.debug("Parameter username is {}", username);
|
||||
logger.debug("Parameter user is {}", user);
|
||||
|
||||
apiImpl.updateUser(username, user)
|
||||
.onSuccess(apiResponse -> {
|
||||
routingContext.response().setStatusCode(apiResponse.getStatusCode());
|
||||
if (apiResponse.hasData()) {
|
||||
routingContext.json(apiResponse.getData());
|
||||
} else {
|
||||
routingContext.response().end();
|
||||
}
|
||||
})
|
||||
.onFailure(routingContext::fail);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package org.openapitools.vertxweb.server.api;
|
||||
|
||||
import org.openapitools.vertxweb.server.model.User;
|
||||
|
||||
import org.openapitools.vertxweb.server.ApiResponse;
|
||||
|
||||
import io.vertx.core.Future;
|
||||
import io.vertx.core.json.JsonObject;
|
||||
import io.vertx.ext.web.handler.impl.HttpStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
// Implement this class
|
||||
|
||||
public class UserApiImpl implements UserApi {
|
||||
public Future<ApiResponse<Void>> createUser(User user) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> createUsersWithArrayInput(List<User> user) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> createUsersWithListInput(List<User> user) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> deleteUser(String username) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<User>> getUserByName(String username) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<String>> loginUser(String username, String password) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> logoutUser() {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
public Future<ApiResponse<Void>> updateUser(String username, User user) {
|
||||
return Future.failedFuture(new HttpStatusException(501));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user