[Java][PlayFramework] Added some CLI Options + many fixes (#5180)

* First commit of the Java Play Framework server generator. It is highly based on Spring so there might me a couple of things that don't make sense (like options or parameters) for the Play Framework.

* Fix suggestions in the PR discussion + add .bat and .sh file as requested.

* Updated Readme.md file

* Remove unused mustache file + fix baseName vs paramName in all the mustache files.

* Fix the compilation error when we have a body which is a list or map. Doesn't fix the problem with the annotation itself.

* Fix the problem with the Http.MultipartFormData.FilePart

* - Add "wrapCalls" and "useSwaggerUI" CLI Options and updated what handleException does to follow Play Framework lifecycle more closely
- Updated all mustache files accordingly
- Updated the sample
- Updates Play Framework version to the latest 2.5.13
This commit is contained in:
Jean-François Côté
2017-03-25 02:36:01 -04:00
committed by wing328
parent 65b6660c55
commit 282b22744c
25 changed files with 230 additions and 94 deletions

View File

@@ -13,18 +13,15 @@ public class ApiCall extends Action<SwaggerUtils.ApiAction> {
@Inject
private ApiCall() {}
public CompletionStage <Result> call(Http.Context ctx) {
public CompletionStage<Result> call(Http.Context ctx) {
try {
//TODO: Do stuff you want to handle with each API call (metrics, logging, etc..)
return delegate.call(ctx);
} catch (Throwable t) {
//TODO: handle error the way you want
return CompletableFuture.completedFuture(handleExceptions(t));
//TODO: log the error in your metric
//We rethrow this error so it will be caught in the ErrorHandler
throw t;
}
}
private Result handleExceptions(Throwable t) {
//TODO: Handle exception that need special response (return a special apimodel, etc..)
return ok();
}
}

View File

@@ -15,6 +15,10 @@
# HOCON will fall back to substituting environment variable:
#mykey = ${JAVA_HOME}
{{#handleExceptions}}
play.http.errorHandler="swagger.ErrorHandler"
{{/handleExceptions}}
## Akka
# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration
# https://www.playframework.com/documentation/latest/JavaAkka#Configuration
@@ -35,8 +39,6 @@ akka {
#log-config-on-start = true
}
api.version="1.0"
## Secret key
# http://www.playframework.com/documentation/latest/ApplicationSecret
# ~~~~~

View File

@@ -9,8 +9,8 @@ scalaVersion := "2.11.7"
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
"org.webjars" % "swagger-ui" % "2.2.10-1"{{#useBeanValidation}},
javaWs{{#useSwaggerUI}},
"org.webjars" % "swagger-ui" % "2.2.10-1"{{/useSwaggerUI}}{{#useBeanValidation}},
"javax.validation" % "validation-api" % "1.1.0.Final"
{{/useBeanValidation}}
)

View File

@@ -0,0 +1,49 @@
package swagger;
import play.*;
import play.api.OptionalSourceMapper;
import play.api.UsefulException;
import play.api.routing.Router;
import play.http.DefaultHttpErrorHandler;
import play.mvc.Http.*;
import play.mvc.*;
import javax.inject.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import static play.mvc.Results.*;
@Singleton
public class ErrorHandler extends DefaultHttpErrorHandler {
@Inject
public ErrorHandler(Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
super(configuration, environment, sourceMapper, routes);
}
@Override
protected CompletionStage<Result> onDevServerError(RequestHeader request, UsefulException exception) {
return CompletableFuture.completedFuture(
handleExceptions(exception)
);
}
@Override
protected CompletionStage<Result> onProdServerError(RequestHeader request, UsefulException exception) {
return CompletableFuture.completedFuture(
handleExceptions(exception)
);
}
@Override
protected void logServerError(RequestHeader request, UsefulException usefulException) {
//Since the error is already handled, we don't want to print anything on the console
//But if you want to have the error printed in the console, just delete this override
}
private Result handleExceptions(Throwable t) {
//TODO: Handle exception that need special response (return a special apimodel, notFound(), etc..)
return ok();
}
}

View File

@@ -14,7 +14,7 @@ import javax.validation.constraints.*;
{{#operations}}
public class {{classname}}ControllerImp {{#useInterfaces}}implements {{classname}}ControllerImpInterface{{/useInterfaces}} {
{{#operation}}
public {{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {
public {{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}} {
//Do your magic!!!
{{#returnType}}return new {{>returnTypesNoVoidNoAbstract}}();{{/returnType}}
}

View File

@@ -19,9 +19,9 @@ import com.fasterxml.jackson.core.type.TypeReference;
import javax.validation.constraints.*;
{{/useBeanValidation}}
{{#handleExceptions}}
{{#wrapCalls}}
import swagger.SwaggerUtils.ApiAction;
{{/handleExceptions}}
{{/wrapCalls}}
{{>generatedAnnotation}}
{{#operations}}
@@ -38,8 +38,8 @@ public class {{classname}}Controller extends Controller {
{{#operation}}
{{#handleExceptions}}@ApiAction{{/handleExceptions}}
public Result {{operationId}}({{#pathParams}}{{>pathParams}}{{#hasMore}},{{/hasMore}}{{/pathParams}}) {{#bodyParams}}throws IOException{{/bodyParams}} {
{{#wrapCalls}}@ApiAction{{/wrapCalls}}
public Result {{operationId}}({{#pathParams}}{{>pathParams}}{{#hasMore}},{{/hasMore}}{{/pathParams}}) {{^handleExceptions}}{{#bodyParams}}throws IOException{{/bodyParams}}{{/handleExceptions}}{{#handleExceptions}}throws Exception{{/handleExceptions}} {
{{#bodyParams}}
{{#collectionFormat}}
//TODO: Maybe implement this in the future if we can support collection in the body params: see bug in swagger-play: https://github.com/swagger-api/swagger-play/issues/130

View File

@@ -15,7 +15,7 @@ import javax.validation.constraints.*;
{{#operations}}
public interface {{classname}}ControllerImpInterface {
{{#operation}}
{{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}};
{{/operation}}
}

View File

@@ -1,2 +1,2 @@
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.10")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.13")

View File

@@ -2,7 +2,9 @@
# This file defines all application routes (Higher priority routes first)
# ~~~~
{{#useSwaggerUI}}
GET /api controllers.ApiDocController.api
{{/useSwaggerUI}}
{{#apiInfo}}
{{#apis}}