forked from loafle/openapi-generator-original
updated to support jaxrs
This commit is contained in:
parent
cab4ffa286
commit
d4122d197f
@ -53,6 +53,8 @@ public class Codegen extends DefaultGenerator {
|
||||
return new ObjcClientCodegen();
|
||||
else if("java".equals(name))
|
||||
return new JavaClientCodegen();
|
||||
else if("jaxrs".equals(name))
|
||||
return new JaxRSServerCodegen();
|
||||
else
|
||||
throw new RuntimeException("unsupported client type");
|
||||
}
|
||||
|
@ -40,4 +40,7 @@ public interface CodegenConfig {
|
||||
String toModelFilename(String name);
|
||||
String toModelImport(String name);
|
||||
String toApiImport(String name);
|
||||
void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations);
|
||||
Map<String, Object> postProcessModels(Map<String, Object> objs);
|
||||
Map<String, Object> postProcessOperations(Map<String, Object> objs);
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import java.util.*;
|
||||
public class CodegenOperation {
|
||||
public Boolean hasParams, returnTypeIsPrimitive, returnSimpleType;
|
||||
public String path, operationId, returnType, httpMethod, returnBaseType,
|
||||
returnContainer, summary, notes;
|
||||
returnContainer, summary, notes, baseName;
|
||||
|
||||
public List<Map<String, String>> consumes, produces;
|
||||
public List<CodegenParameter> allParams = new ArrayList<CodegenParameter>();
|
||||
@ -18,6 +18,8 @@ public class CodegenOperation {
|
||||
public List<CodegenParameter> queryParams = new ArrayList<CodegenParameter>();
|
||||
public List<CodegenParameter> headerParams = new ArrayList<CodegenParameter>();
|
||||
public List<CodegenParameter> formParams = new ArrayList<CodegenParameter>();
|
||||
public List<String> tags;
|
||||
public List<CodegenResponse> responses = new ArrayList<CodegenResponse>();
|
||||
|
||||
public Set<String> imports = new HashSet<String>();
|
||||
|
||||
|
@ -2,5 +2,5 @@ package com.wordnik.swagger.codegen;
|
||||
|
||||
public class CodegenParameter {
|
||||
public Boolean hasMore = null, isContainer = null, secondaryParam = null;
|
||||
public String baseName, paramName, dataType, collectionFormat;
|
||||
public String baseName, paramName, dataType, collectionFormat, description;
|
||||
}
|
@ -22,6 +22,16 @@ public class DefaultCodegen {
|
||||
protected Map<String, Object> additionalProperties = new HashMap<String, Object>();
|
||||
protected List<SupportingFile> supportingFiles = new ArrayList<SupportingFile>();
|
||||
|
||||
// override with any special post-processing
|
||||
public Map<String, Object> postProcessModels(Map<String, Object> objs) {
|
||||
return objs;
|
||||
}
|
||||
|
||||
// override with any special post-processing
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
return objs;
|
||||
}
|
||||
|
||||
public Set<String> defaultIncludes() {
|
||||
return defaultIncludes;
|
||||
}
|
||||
@ -381,8 +391,6 @@ public class DefaultCodegen {
|
||||
if(languageSpecificPrimitives().contains(type))
|
||||
property.isPrimitiveType = true;
|
||||
}
|
||||
if("id".equals(property.name))
|
||||
Json.prettyPrint(property);
|
||||
return property;
|
||||
}
|
||||
|
||||
@ -397,6 +405,7 @@ public class DefaultCodegen {
|
||||
op.operationId = operationId;
|
||||
op.summary = operation.getSummary();
|
||||
op.notes = operation.getDescription();
|
||||
op.tags = operation.getTags();
|
||||
|
||||
Response methodResponse = null;
|
||||
|
||||
@ -438,6 +447,23 @@ public class DefaultCodegen {
|
||||
if(methodResponse == null && operation.getResponses().keySet().contains("default")) {
|
||||
methodResponse = operation.getResponses().get("default");
|
||||
}
|
||||
for(String responseCode: operation.getResponses().keySet()) {
|
||||
Response response = operation.getResponses().get(responseCode);
|
||||
if(response != methodResponse) {
|
||||
CodegenResponse r = new CodegenResponse();
|
||||
if("default".equals(responseCode))
|
||||
r.code = "0";
|
||||
else
|
||||
r.code = responseCode;
|
||||
r.message = response.getDescription();
|
||||
r.schema = response.getSchema();
|
||||
op.responses.add(r);
|
||||
}
|
||||
for(int i = 0; i < op.responses.size() - 1; i++) {
|
||||
CodegenResponse r = op.responses.get(i);
|
||||
r.hasMore = new Boolean(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(methodResponse != null && methodResponse.getSchema() != null) {
|
||||
@ -490,6 +516,7 @@ public class DefaultCodegen {
|
||||
for(Parameter param : parameters) {
|
||||
CodegenParameter p = new CodegenParameter();
|
||||
p.baseName = param.getName();
|
||||
p.description = param.getDescription();
|
||||
|
||||
if(param instanceof SerializableParameter) {
|
||||
SerializableParameter qp = (SerializableParameter) param;
|
||||
@ -610,4 +637,15 @@ public class DefaultCodegen {
|
||||
}
|
||||
return objs;
|
||||
}
|
||||
|
||||
|
||||
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
|
||||
List<CodegenOperation> opList = operations.get(tag);
|
||||
if(opList == null) {
|
||||
opList = new ArrayList<CodegenOperation>();
|
||||
operations.put(tag, opList);
|
||||
}
|
||||
opList.add(co);
|
||||
co.baseName = tag;
|
||||
}
|
||||
}
|
@ -43,12 +43,17 @@ public class DefaultGenerator implements Generator {
|
||||
modelMap.put(name, model);
|
||||
models = processModels(config, modelMap);
|
||||
models.putAll(config.additionalProperties());
|
||||
for(String templateName: config.modelTemplateFiles().keySet()) {
|
||||
for(String templateName : config.modelTemplateFiles().keySet()) {
|
||||
String suffix = config.modelTemplateFiles().get(templateName);
|
||||
String filename = config.modelFileFolder() + File.separator + config.toModelFilename(name) + suffix;
|
||||
|
||||
String template = readTemplate(config.templateDir() + File.separator + templateName);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
public Reader getTemplate (String name) {
|
||||
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
||||
};
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
|
||||
@ -56,13 +61,14 @@ public class DefaultGenerator implements Generator {
|
||||
}
|
||||
}
|
||||
// apis
|
||||
Map<String, List<CodegenOperation>> paths = groupPaths(swagger.getPaths());
|
||||
for(String tag: paths.keySet()) {
|
||||
Map<String, List<CodegenOperation>> paths = processPaths(swagger.getPaths());
|
||||
for(String tag : paths.keySet()) {
|
||||
List<CodegenOperation> ops = paths.get(tag);
|
||||
|
||||
operations = processOperations(config, tag, ops);
|
||||
operations.putAll(config.additionalProperties());
|
||||
for(String templateName: config.apiTemplateFiles().keySet()) {
|
||||
operations.put("baseName", tag);
|
||||
for(String templateName : config.apiTemplateFiles().keySet()) {
|
||||
String suffix = config.apiTemplateFiles().get(templateName);
|
||||
String filename = config.apiFileFolder() +
|
||||
File.separator +
|
||||
@ -71,6 +77,12 @@ public class DefaultGenerator implements Generator {
|
||||
|
||||
String template = readTemplate(config.templateDir() + File.separator + templateName);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
public Reader getTemplate (String name) {
|
||||
System.out.println("loading template " + name);
|
||||
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
||||
};
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
|
||||
@ -81,7 +93,8 @@ public class DefaultGenerator implements Generator {
|
||||
// supporting files
|
||||
Map<String, Object> bundle = new HashMap<String, Object>();
|
||||
bundle.putAll(config.additionalProperties());
|
||||
for(SupportingFile support: config.supportingFiles()) {
|
||||
bundle.put("apiPackage", config.apiPackage());
|
||||
for(SupportingFile support : config.supportingFiles()) {
|
||||
String outputFolder = config.outputFolder();
|
||||
if(support.folder != null && !"".equals(support.folder))
|
||||
outputFolder += File.separator + support.folder;
|
||||
@ -93,6 +106,11 @@ public class DefaultGenerator implements Generator {
|
||||
if(support.templateFile.endsWith("mustache")) {
|
||||
String template = readTemplate(config.templateDir() + File.separator + support.templateFile);
|
||||
Template tmpl = Mustache.compiler()
|
||||
.withLoader(new Mustache.TemplateLoader() {
|
||||
public Reader getTemplate (String name) {
|
||||
return getTemplateReader(config.templateDir() + File.separator + name + ".mustache");
|
||||
};
|
||||
})
|
||||
.defaultValue("")
|
||||
.compile(template);
|
||||
|
||||
@ -110,12 +128,12 @@ public class DefaultGenerator implements Generator {
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, List<CodegenOperation>> groupPaths(Map<String, Path> paths) {
|
||||
public Map<String, List<CodegenOperation>> processPaths(Map<String, Path> paths) {
|
||||
// group by tag, create a Default grouping if none
|
||||
Map<String, List<CodegenOperation>> ops = new HashMap<String, List<CodegenOperation>>();
|
||||
List<String> tags = null;
|
||||
|
||||
for(String resourcePath: paths.keySet()) {
|
||||
for(String resourcePath : paths.keySet()) {
|
||||
Path path = paths.get(resourcePath);
|
||||
processOperation(resourcePath, "get", path.getGet(), ops);
|
||||
processOperation(resourcePath, "put", path.getPut(), ops);
|
||||
@ -135,14 +153,12 @@ public class DefaultGenerator implements Generator {
|
||||
tags.add("default");
|
||||
}
|
||||
|
||||
for(String tag: tags) {
|
||||
List<CodegenOperation> opList = operations.get(tag);
|
||||
if(opList == null) {
|
||||
opList = new ArrayList<CodegenOperation>();
|
||||
operations.put(tag, opList);
|
||||
}
|
||||
for(String tag : tags) {
|
||||
CodegenOperation co = config.fromOperation(resourcePath, httpMethod, operation);
|
||||
opList.add(co);
|
||||
co.tags = new ArrayList<String>();
|
||||
co.tags.add(tag);
|
||||
|
||||
config.addOperationToGroup(tag, resourcePath, operation, co, operations);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -164,14 +180,27 @@ public class DefaultGenerator implements Generator {
|
||||
}
|
||||
|
||||
public String readTemplate(String name) {
|
||||
try{
|
||||
Reader reader = getTemplateReader(name);
|
||||
if(reader == null)
|
||||
throw new RuntimeException("no file found");
|
||||
java.util.Scanner s = new java.util.Scanner(reader).useDelimiter("\\A");
|
||||
return s.hasNext() ? s.next() : "";
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new RuntimeException("can't load template " + name);
|
||||
}
|
||||
|
||||
public Reader getTemplateReader(String name) {
|
||||
try{
|
||||
InputStream is = this.getClass().getClassLoader().getResourceAsStream(name);
|
||||
if(is == null)
|
||||
is = new FileInputStream(new File(name));
|
||||
if(is == null)
|
||||
throw new RuntimeException("no file found");
|
||||
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
|
||||
return s.hasNext() ? s.next() : "";
|
||||
return new InputStreamReader(is);
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
@ -205,7 +234,7 @@ public class DefaultGenerator implements Generator {
|
||||
}
|
||||
|
||||
operations.put("imports", imports);
|
||||
|
||||
config.postProcessOperations(operations);
|
||||
return operations;
|
||||
}
|
||||
|
||||
@ -240,6 +269,8 @@ public class DefaultGenerator implements Generator {
|
||||
}
|
||||
|
||||
objs.put("imports", imports);
|
||||
config.postProcessModels(objs);
|
||||
|
||||
return objs;
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
package com.wordnik.swagger.codegen.languages;
|
||||
|
||||
import com.wordnik.swagger.models.Operation;
|
||||
import com.wordnik.swagger.models.Path;
|
||||
import com.wordnik.swagger.util.Json;
|
||||
import com.wordnik.swagger.codegen.*;
|
||||
import com.wordnik.swagger.models.properties.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.File;
|
||||
|
||||
public class JaxRSServerCodegen extends JavaClientCodegen implements CodegenConfig {
|
||||
protected String invokerPackage = "com.wordnik.api";
|
||||
protected String groupId = "com.wordnik";
|
||||
protected String artifactId = "swagger-server";
|
||||
protected String artifactVersion = "1.0.0";
|
||||
protected String sourceFolder = "src/main/java";
|
||||
protected String title = "Swagger Server";
|
||||
|
||||
public JaxRSServerCodegen() {
|
||||
super();
|
||||
outputFolder = "generated-code/javaJaxRS";
|
||||
modelTemplateFiles.put("model.mustache", ".java");
|
||||
apiTemplateFiles.put("api.mustache", ".java");
|
||||
templateDir = "src/main/resources/JavaJaxRS";
|
||||
apiPackage = "com.wordnik.api";
|
||||
modelPackage = "com.wordnik.model";
|
||||
|
||||
additionalProperties.put("invokerPackage", invokerPackage);
|
||||
additionalProperties.put("groupId", groupId);
|
||||
additionalProperties.put("artifactId", artifactId);
|
||||
additionalProperties.put("artifactVersion", artifactVersion);
|
||||
additionalProperties.put("title", title);
|
||||
|
||||
supportingFiles.clear();
|
||||
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
|
||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||
supportingFiles.add(new SupportingFile("ApiException.mustache",
|
||||
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiException.java"));
|
||||
supportingFiles.add(new SupportingFile("ApiOriginFilter.mustache",
|
||||
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiOriginFilter.java"));
|
||||
supportingFiles.add(new SupportingFile("ApiResponseMessage.mustache",
|
||||
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "ApiResponseMessage.java"));
|
||||
supportingFiles.add(new SupportingFile("NotFoundException.mustache",
|
||||
(sourceFolder + File.separator + apiPackage).replace(".", java.io.File.separator), "NotFoundException.java"));
|
||||
supportingFiles.add(new SupportingFile("web.mustache",
|
||||
("src/main/webapp/WEB-INF"), "web.xml"));
|
||||
|
||||
languageSpecificPrimitives = new HashSet<String>(
|
||||
Arrays.asList(
|
||||
"String",
|
||||
"boolean",
|
||||
"Boolean",
|
||||
"Double",
|
||||
"Integer",
|
||||
"Long",
|
||||
"Float")
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
|
||||
String basePath = resourcePath;
|
||||
if(basePath.startsWith("/"))
|
||||
basePath = basePath.substring(1);
|
||||
int pos = basePath.indexOf("/");
|
||||
if(pos > 0)
|
||||
basePath = basePath.substring(0, pos);
|
||||
|
||||
if(basePath == "")
|
||||
basePath = "default";
|
||||
else {
|
||||
if(co.path.startsWith("/" + basePath))
|
||||
co.path = co.path.substring(("/" + basePath).length());
|
||||
}
|
||||
List<CodegenOperation> opList = operations.get(basePath);
|
||||
if(opList == null) {
|
||||
opList = new ArrayList<CodegenOperation>();
|
||||
operations.put(basePath, opList);
|
||||
}
|
||||
opList.add(co);
|
||||
co.baseName = basePath;
|
||||
}
|
||||
|
||||
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
|
||||
Map<String, Object> operations = (Map<String, Object>)objs.get("operations");
|
||||
if(operations != null) {
|
||||
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
|
||||
for(CodegenOperation operation : ops) {
|
||||
if(operation.returnType == null)
|
||||
operation.returnType = "Void";
|
||||
else if(operation.returnType.startsWith("List")) {
|
||||
String rt = operation.returnType;
|
||||
int end = rt.lastIndexOf(">");
|
||||
if(end > 0) {
|
||||
operation.returnType = rt.substring("List<".length(), end);
|
||||
operation.returnContainer = "List";
|
||||
}
|
||||
}
|
||||
else if(operation.returnType.startsWith("Map")) {
|
||||
String rt = operation.returnType;
|
||||
int end = rt.lastIndexOf(">");
|
||||
if(end > 0) {
|
||||
operation.returnType = rt.substring("Map<".length(), end);
|
||||
operation.returnContainer = "Map";
|
||||
}
|
||||
}
|
||||
else if(operation.returnType.startsWith("Set")) {
|
||||
String rt = operation.returnType;
|
||||
int end = rt.lastIndexOf(">");
|
||||
if(end > 0) {
|
||||
operation.returnType = rt.substring("Set<".length(), end);
|
||||
operation.returnContainer = "Set";
|
||||
}
|
||||
}
|
||||
// Json.prettyPrint(operation);
|
||||
// if(return)
|
||||
}
|
||||
}
|
||||
// Json.prettyPrint(objs);
|
||||
return objs;
|
||||
}
|
||||
}
|
9
src/main/resources/JavaJaxRS/ApiException.mustache
Normal file
9
src/main/resources/JavaJaxRS/ApiException.mustache
Normal file
@ -0,0 +1,9 @@
|
||||
package {{apiPackage}};
|
||||
|
||||
public class ApiException extends Exception{
|
||||
private int code;
|
||||
public ApiException (int code, String msg) {
|
||||
super(msg);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
26
src/main/resources/JavaJaxRS/ApiOriginFilter.mustache
Normal file
26
src/main/resources/JavaJaxRS/ApiOriginFilter.mustache
Normal file
@ -0,0 +1,26 @@
|
||||
package {{apiPackage}};
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class ApiOriginFilter implements javax.servlet.Filter {
|
||||
@Override
|
||||
public void doFilter(ServletRequest request, ServletResponse response,
|
||||
FilterChain chain) throws IOException, ServletException {
|
||||
HttpServletResponse res = (HttpServletResponse) response;
|
||||
res.addHeader("Access-Control-Allow-Origin", "*");
|
||||
res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
|
||||
res.addHeader("Access-Control-Allow-Headers", "Content-Type");
|
||||
chain.doFilter(request, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(FilterConfig filterConfig) throws ServletException {
|
||||
}
|
||||
}
|
68
src/main/resources/JavaJaxRS/ApiResponseMessage.mustache
Normal file
68
src/main/resources/JavaJaxRS/ApiResponseMessage.mustache
Normal file
@ -0,0 +1,68 @@
|
||||
package {{apiPackage}};
|
||||
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
|
||||
@javax.xml.bind.annotation.XmlRootElement
|
||||
public class ApiResponseMessage {
|
||||
public static final int ERROR = 1;
|
||||
public static final int WARNING = 2;
|
||||
public static final int INFO = 3;
|
||||
public static final int OK = 4;
|
||||
public static final int TOO_BUSY = 5;
|
||||
|
||||
int code;
|
||||
String type;
|
||||
String message;
|
||||
|
||||
public ApiResponseMessage(){}
|
||||
|
||||
public ApiResponseMessage(int code, String message){
|
||||
this.code = code;
|
||||
switch(code){
|
||||
case ERROR:
|
||||
setType("error");
|
||||
break;
|
||||
case WARNING:
|
||||
setType("warning");
|
||||
break;
|
||||
case INFO:
|
||||
setType("info");
|
||||
break;
|
||||
case OK:
|
||||
setType("ok");
|
||||
break;
|
||||
case TOO_BUSY:
|
||||
setType("too busy");
|
||||
break;
|
||||
default:
|
||||
setType("unknown");
|
||||
break;
|
||||
}
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@XmlTransient
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
9
src/main/resources/JavaJaxRS/NotFoundException.mustache
Normal file
9
src/main/resources/JavaJaxRS/NotFoundException.mustache
Normal file
@ -0,0 +1,9 @@
|
||||
package {{apiPackage}};
|
||||
|
||||
public class NotFoundException extends ApiException {
|
||||
private int code;
|
||||
public NotFoundException (int code, String msg) {
|
||||
super(code, msg);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
10
src/main/resources/JavaJaxRS/README.mustache
Normal file
10
src/main/resources/JavaJaxRS/README.mustache
Normal file
@ -0,0 +1,10 @@
|
||||
# Swagger generated server
|
||||
|
||||
## Overview
|
||||
This server was generated by the [swagger-codegen](https://github.com/wordnik/swagger-codegen) project. By using the
|
||||
[swagger-spec](https://github.com/wordnik/swagger-core/wiki) from a remote server, you can easily generate a server stub. This
|
||||
is an example of building a swagger-enabled scalatra server.
|
||||
|
||||
This example uses the [scalatra](http://scalatra.org/) framework. To see how to make this your own, look here:
|
||||
|
||||
[README](https://github.com/wordnik/swagger-codegen/tree/master/samples/server-generator/scalatra)
|
36
src/main/resources/JavaJaxRS/api.mustache
Normal file
36
src/main/resources/JavaJaxRS/api.mustache
Normal file
@ -0,0 +1,36 @@
|
||||
package {{package}};
|
||||
|
||||
import com.wordnik.swagger.annotations.*;
|
||||
|
||||
{{#imports}}import {{import}};
|
||||
{{/imports}}
|
||||
|
||||
import java.util.List;
|
||||
import {{package}}.NotFoundException;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.*;
|
||||
|
||||
@Path("/{{baseName}}")
|
||||
@Api(value = "/{{baseName}}", description = "the {{baseName}} API")
|
||||
@Produces({"application/json"})
|
||||
{{#operations}}
|
||||
public class {{classname}} {
|
||||
{{#operation}}
|
||||
@{{httpMethod}}
|
||||
@Path("{{path}}")
|
||||
@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}", response = {{{returnType}}}.class {{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}})
|
||||
@ApiResponses(value = { {{#responses}}
|
||||
@ApiResponse(code = {{{code}}}, message = "{{{message}}}"){{#hasMore}},
|
||||
{{/hasMore}}{{/responses}} })
|
||||
|
||||
public Response {{nickname}}(
|
||||
{{#allParams}}{{>queryParams}}{{>pathParams}} {{>headerParams}} {{>bodyParams}} {{#hasMore}},{{/hasMore}}{{/allParams}})
|
||||
throws NotFoundException {
|
||||
// do some magic!
|
||||
return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build();
|
||||
}
|
||||
|
||||
{{/operation}}
|
||||
}
|
||||
{{/operations}}
|
1
src/main/resources/JavaJaxRS/bodyParams.mustache
Normal file
1
src/main/resources/JavaJaxRS/bodyParams.mustache
Normal file
@ -0,0 +1 @@
|
||||
{{#bodyParams}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{newline}}{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{newline}}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{newline}}{{/defaultValue}}) {{{dataType}}} {{paramName}}{{/bodyParams}}
|
1
src/main/resources/JavaJaxRS/headerParams.mustache
Normal file
1
src/main/resources/JavaJaxRS/headerParams.mustache
Normal file
@ -0,0 +1 @@
|
||||
{{#headerParams}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{newline}}{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{newline}}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{newline}}{{/defaultValue}})@HeaderParam("{{paramName}}"){{newline}} {{{dataType}}} {{paramName}}{{/headerParams}}
|
42
src/main/resources/JavaJaxRS/model.mustache
Normal file
42
src/main/resources/JavaJaxRS/model.mustache
Normal file
@ -0,0 +1,42 @@
|
||||
package {{package}};
|
||||
|
||||
{{#imports}}import {{import}};
|
||||
{{/imports}}
|
||||
{{#models}}
|
||||
|
||||
{{#model}}{{#description}}
|
||||
/**
|
||||
* {{description}}
|
||||
**/{{/description}}
|
||||
public class {{classname}} { {{#vars}}
|
||||
/**{{#description}}
|
||||
* {{{description}}}{{/description}}
|
||||
* required: {{required}}{{#minimum}}
|
||||
* minimum: {{minimum}}{{/minimum}}{{#maximum}}
|
||||
* maximum: {{maximum}}{{/maximum}}
|
||||
**/
|
||||
private {{{datatype}}} {{name}} = {{{defaultValue}}};{{#allowableValues}}
|
||||
|
||||
//{{^min}}public enum {{name}}Enum { {{#values}} {{.}}, {{/values}} };
|
||||
{{/min}}{{/allowableValues}}{{/vars}}
|
||||
|
||||
{{#vars}}public {{{datatype}}} {{getter}}() {
|
||||
return {{name}};
|
||||
}
|
||||
public void {{setter}}({{{datatype}}} {{name}}) {
|
||||
this.{{name}} = {{name}};
|
||||
}
|
||||
|
||||
{{/vars}}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("class {{classname}} {\n");
|
||||
{{#vars}}sb.append(" {{name}}: ").append({{name}}).append("\n");
|
||||
{{/vars}}sb.append("}\n");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
{{/model}}
|
||||
{{/models}}
|
1
src/main/resources/JavaJaxRS/pathParams.mustache
Normal file
1
src/main/resources/JavaJaxRS/pathParams.mustache
Normal file
@ -0,0 +1 @@
|
||||
{{#pathParams}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{newline}}{{/required}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{newline}}{{/allowableValues}} {{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{newline}}{{/defaultValue}})@PathParam("{{paramName}}"){{newline}} {{{dataType}}} {{paramName}}{{/pathParams}}
|
126
src/main/resources/JavaJaxRS/pom.mustache
Normal file
126
src/main/resources/JavaJaxRS/pom.mustache
Normal file
@ -0,0 +1,126 @@
|
||||
<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>
|
||||
<packaging>jar</packaging>
|
||||
<name>{{artifactId}}</name>
|
||||
<version>{{artifactVersion}}</version>
|
||||
<build>
|
||||
<sourceDirectory>src/main/java</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.mortbay.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>${jetty-version}</version>
|
||||
<configuration>
|
||||
<webAppConfig>
|
||||
<contextPath>/</contextPath>
|
||||
</webAppConfig>
|
||||
<webAppSourceDirectory>target/${project.artifactId}-${project.version}</webAppSourceDirectory>
|
||||
<webDefaultXml>${project.basedir}/conf/jetty/webdefault.xml</webDefaultXml>
|
||||
<stopPort>8079</stopPort>
|
||||
<stopKey>stopit</stopKey>
|
||||
<connectors>
|
||||
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
|
||||
<port>8002</port>
|
||||
<maxIdleTime>60000</maxIdleTime>
|
||||
<confidentialPort>8443</confidentialPort>
|
||||
</connector>
|
||||
</connectors>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>start-jetty</id>
|
||||
<phase>pre-integration-test</phase>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<scanIntervalSeconds>0</scanIntervalSeconds>
|
||||
<daemon>true</daemon>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>stop-jetty</id>
|
||||
<phase>post-integration-test</phase>
|
||||
<goals>
|
||||
<goal>stop</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.wordnik</groupId>
|
||||
<artifactId>swagger-jaxrs</artifactId>
|
||||
<version>${swagger-core-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-core</artifactId>
|
||||
<version>${jersey-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-json</artifactId>
|
||||
<version>${jersey-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey</groupId>
|
||||
<artifactId>jersey-servlet</artifactId>
|
||||
<version>${jersey-version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.scalatest</groupId>
|
||||
<artifactId>scalatest_2.9.1</artifactId>
|
||||
<version>${scala-test-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>servlet-api</artifactId>
|
||||
<version>${servlet-api-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<swagger-core-version>1.5.0-SNAPSHOT</swagger-core-version>
|
||||
<jetty-version>8.1.11.v20130520</jetty-version>
|
||||
<jersey-version>1.13</jersey-version>
|
||||
<slf4j-version>1.6.3</slf4j-version>
|
||||
<scala-test-version>1.6.1</scala-test-version>
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<servlet-api-version>2.5</servlet-api-version>
|
||||
</properties>
|
||||
</project>
|
1
src/main/resources/JavaJaxRS/project/build.properties
Normal file
1
src/main/resources/JavaJaxRS/project/build.properties
Normal file
@ -0,0 +1 @@
|
||||
sbt.version=0.12.0
|
9
src/main/resources/JavaJaxRS/project/plugins.sbt
Normal file
9
src/main/resources/JavaJaxRS/project/plugins.sbt
Normal file
@ -0,0 +1,9 @@
|
||||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.8.4")
|
||||
|
||||
libraryDependencies <+= sbtVersion(v => v match {
|
||||
case "0.11.0" => "com.github.siasia" %% "xsbt-web-plugin" % "0.11.0-0.2.8"
|
||||
case "0.11.1" => "com.github.siasia" %% "xsbt-web-plugin" % "0.11.1-0.2.10"
|
||||
case "0.11.2" => "com.github.siasia" %% "xsbt-web-plugin" % "0.11.2-0.2.11"
|
||||
case "0.11.3" => "com.github.siasia" %% "xsbt-web-plugin" % "0.11.3-0.2.11.1"
|
||||
case x if (x.startsWith("0.12")) => "com.github.siasia" %% "xsbt-web-plugin" % "0.12.0-0.2.11.1"
|
||||
})
|
1
src/main/resources/JavaJaxRS/queryParams.mustache
Normal file
1
src/main/resources/JavaJaxRS/queryParams.mustache
Normal file
@ -0,0 +1 @@
|
||||
{{#queryParams}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{newline}}{{/required}} {{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{newline}}{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{newline}}{{/defaultValue}})@QueryParam("{{paramName}}"){{newline}} {{{dataType}}} {{paramName}}{{/queryParams}}
|
54
src/main/resources/JavaJaxRS/web.mustache
Normal file
54
src/main/resources/JavaJaxRS/web.mustache
Normal file
@ -0,0 +1,54 @@
|
||||
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
|
||||
xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
|
||||
|
||||
<servlet>
|
||||
<servlet-name>jersey</servlet-name>
|
||||
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
|
||||
<init-param>
|
||||
<param-name>com.sun.jersey.config.property.packages</param-name>
|
||||
<param-value>com.wordnik.swagger.jaxrs.json;com.wordnik.swagger.jaxrs.listing;{{apiPackage}}</param-value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
|
||||
<param-value>com.sun.jersey.api.container.filter.PostReplaceFilter</param-value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
|
||||
<param-value>true</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>DefaultJaxrsConfig</servlet-name>
|
||||
<servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
|
||||
<init-param>
|
||||
<param-name>api.version</param-name>
|
||||
<param-value>1.0.0</param-value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<param-name>swagger.api.title</param-name>
|
||||
<param-value>{{{title}}}</param-value>
|
||||
</init-param>
|
||||
<init-param>
|
||||
<param-name>swagger.api.basepath</param-name>
|
||||
<param-value>http://localhost:8002</param-value>
|
||||
</init-param>
|
||||
<load-on-startup>2</load-on-startup>
|
||||
</servlet>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>jersey</servlet-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</servlet-mapping>
|
||||
<filter>
|
||||
<filter-name>ApiOriginFilter</filter-name>
|
||||
<filter-class>com.wordnik.api.ApiOriginFilter</filter-class>
|
||||
</filter>
|
||||
<filter-mapping>
|
||||
<filter-name>ApiOriginFilter</filter-name>
|
||||
<url-pattern>/*</url-pattern>
|
||||
</filter-mapping>
|
||||
</web-app>
|
Loading…
x
Reference in New Issue
Block a user