JavaSpring.apiDelegate.mustache Maven / Gradle / Ivy
package {{package}};
{{#imports}}import {{import}};
{{/imports}}
{{#jdk8}}
import com.fasterxml.jackson.databind.ObjectMapper;
{{/jdk8}}
import io.swagger.annotations.*;
{{#jdk8}}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
{{/jdk8}}
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;
{{#jdk8}}
import java.io.IOException;
{{/jdk8}}
{{#jdk8}}
import javax.servlet.http.HttpServletRequest;
{{/jdk8}}
import java.util.List;
{{#jdk8}}
import java.util.Optional;
{{/jdk8}}
{{^jdk8}}
{{#useOptional}}
import java.util.Optional;
{{/useOptional}}
{{/jdk8}}
{{#async}}
import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}};
{{/async}}
{{#operations}}
/**
* A delegate to be called by the {@link {{classname}}Controller}}.
* Implement this interface with a {@link org.springframework.stereotype.Service} annotated class.
*/
{{>generatedAnnotation}}
public interface {{classname}}Delegate {
{{#jdk8}}
Logger log = LoggerFactory.getLogger({{classname}}.class);
default Optional getObjectMapper() {
return Optional.empty();
}
default Optional getRequest() {
return Optional.empty();
}
default Optional getAcceptHeader() {
return getRequest().map(r -> r.getHeader("Accept"));
}
{{/jdk8}}
{{#operation}}
/**
* @see {{classname}}#{{operationId}}
*/
{{#defaultInterfaces}}default {{/defaultInterfaces}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{^isFile}} {{>optionalDataType}} {{/isFile}}{{#isFile}}MultipartFile{{/isFile}} {{paramName}}{{#hasMore}},
{{/hasMore}}{{/allParams}}){{^defaultInterfaces}};{{/defaultInterfaces}}{{#defaultInterfaces}} {
if(getObjectMapper().isPresent() && getAcceptHeader().isPresent()) {
{{#examples}}
if (getAcceptHeader().get().contains("{{{contentType}}}")) {
try {
return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(getObjectMapper().get().readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class), HttpStatus.NOT_IMPLEMENTED){{#async}}){{/async}};
} catch (IOException e) {
log.error("Couldn't serialize response for content type {{{contentType}}}", e);
return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR){{#async}}){{/async}};
}
}
{{/examples}}
} else {
log.warn("ObjectMapper or HttpServletRequest not configured in default {{classname}} interface so no example is generated");
}
return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED){{#async}}){{/async}};
}{{/defaultInterfaces}}
{{/operation}}
}
{{/operations}}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy