handlebars.JavaSpring.apiController.mustache Maven / Gradle / Ivy
package {{package}};
{{#fullController}}
{{#imports}}import {{import}};
{{/imports}}
{{/fullController}}
{{^isDelegate}}
import com.fasterxml.jackson.databind.ObjectMapper;
{{/isDelegate}}
{{#fullController}}
{{#useOas2}}
import io.swagger.annotations.*;
{{/useOas2}}
{{^useOas2}}
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
{{/useOas2}}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
{{/fullController}}
import org.springframework.web.bind.annotation.RestController;
{{#fullController}}
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
{{^useOas2}}
import org.springframework.web.bind.annotation.RestController;
{{/useOas2}}
import org.springframework.web.multipart.MultipartFile;
{{#useBeanValidation}}
{{#jakarta}}
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
{{/jakarta}}
{{^jakarta}}
import javax.validation.Valid;
import javax.validation.constraints.*;
{{/jakarta}}
{{/useBeanValidation}}
{{/fullController}}
{{^isDelegate}}
{{#jakarta}}
import jakarta.servlet.http.HttpServletRequest;
{{/jakarta}}
{{^jakarta}}
import javax.servlet.http.HttpServletRequest;
{{/jakarta}}
{{#isJava8or11}}
import java.util.Optional;
{{/isJava8or11}}
{{/isDelegate}}
{{^jdk8-no-delegate}}
{{#useOptional}}
import java.util.Optional;
{{/useOptional}}
{{/jdk8-no-delegate}}
{{#fullController}}
{{^isDelegate}}
import java.io.IOException;
{{/isDelegate}}
import java.util.List;
import java.util.Map;
{{#async}}
import java.util.concurrent.Callable;
{{/async}}
{{/fullController}}
{{>generatedAnnotation}}
{{#useOas2}}
@RestController
{{/useOas2}}
{{^useOas2}}
@RestController
{{/useOas2}}
{{#operations}}
public class {{classname}}Controller implements {{classname}} {
{{#isDelegate}}
private final {{classname}}Delegate delegate;
@org.springframework.beans.factory.annotation.Autowired
public {{classname}}Controller({{classname}}Delegate delegate) {
this.delegate = delegate;
}
{{#isJava8or11}}
@Override
public {{classname}}Delegate getDelegate() {
return delegate;
}
{{/isJava8or11}}
{{/isDelegate}}
{{^isDelegate}}
{{#fullController}}
private static final Logger log = LoggerFactory.getLogger({{classname}}Controller.class);
{{/fullController}}
private final ObjectMapper objectMapper;
private final HttpServletRequest request;
@org.springframework.beans.factory.annotation.Autowired
public {{classname}}Controller(ObjectMapper objectMapper, HttpServletRequest request) {
this.objectMapper = objectMapper;
this.request = request;
}
{{#isJava8or11}}
@Override
public Optional getObjectMapper() {
return Optional.ofNullable(objectMapper);
}
@Override
public Optional getRequest() {
return Optional.ofNullable(request);
}
{{/isJava8or11}}
{{/isDelegate}}
{{#fullController}}
{{#operation}}
{{#contents}}
{{#@first}}
public {{#async}}Callable<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{#hasMore}},{{/hasMore}}{{/parameters}}) {
{{^isDelegate}}
{{^async}}
String accept = request.getHeader("Accept");
{{#examples}}
if (accept != null && accept.contains("{{{contentType}}}")) {
try {
return new ResponseEntity<{{>returnTypes}}>(objectMapper.readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type {{{contentType}}}", e);
return new ResponseEntity<{{>returnTypes}}>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
{{/examples}}
return new ResponseEntity<{{>returnTypes}}>(HttpStatus.NOT_IMPLEMENTED);
{{/async}}
{{#async}}
return new CallablereturnTypes}}>>() {
@Override
public ResponseEntity<{{>returnTypes}}> call() {
String accept = request.getHeader("Accept");
{{#examples}}
if (accept != null && accept.contains("{{{contentType}}}")) {
try {
return new ResponseEntity<{{>returnTypes}}>(objectMapper.readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class), HttpStatus.NOT_IMPLEMENTED);
} catch (IOException e) {
log.error("Couldn't serialize response for content type {{{contentType}}}", e);
return new ResponseEntity<{{>returnTypes}}>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
{{/examples}}
return new ResponseEntity<{{>returnTypes}}>(HttpStatus.NOT_IMPLEMENTED);
}
};
{{/async}}
{{/isDelegate}}
{{#isDelegate}}
return delegate.{{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
{{/isDelegate}}
}
{{/@first}}
{{/contents}}
{{/operation}}
{{/fullController}}
}
{{/operations}}