commons.rest-from-swagger.apiController.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaffold-clean-architecture Show documentation
Show all versions of scaffold-clean-architecture Show documentation
Gradle plugin to create a clean application in Java that already works, It follows our best practices!
package {{package}};
{{#imports}}import {{import}};
{{/imports}}
{{#lombok}}
import lombok.extern.log4j.Log4j2;
import lombok.AllArgsConstructor;
{{/lombok}}
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
{{#async}}
import reactor.core.publisher.Mono;
{{/async}}
{{#useBeanValidation}}
{{#jakarta}}
import jakarta.validation.Valid;
import jakarta.validation.constraints.*;
{{/jakarta}}
{{^jakarta}}
import javax.validation.Valid;
import javax.validation.constraints.*;
{{/jakarta}}
{{/useBeanValidation}}
import java.util.List;
import java.util.Map;
{{#lombok}}
@Log4j2
@AllArgsConstructor
{{/lombok}}
@RestController
{{#operations}}
public class {{classname}}Controller {
// private final UseCase someUseCase;
{{^lombok}}
private static final Logger log = LoggerFactory.getLogger({{classname}}Controller.class);
{{/lombok}}
{{#operation}}
{{#contents}}
{{#@first}}
@RequestMapping(value = "{{{path}}}",{{#singleContentTypes}}{{#hasProduces}}
produces = "{{{vendorExtensions.x-accepts}}}", {{/hasProduces}}{{#hasConsumes}}
consumes = "{{{vendorExtensions.x-contentType}}}",{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}}
produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}}
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }, {{/hasConsumes}}{{/singleContentTypes}}
method = RequestMethod.{{httpMethod}})
public {{#async}}Mono<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{#hasMore}},{{/hasMore}}{{/parameters}}) {
{{#async}}
return {{operationId}}Mock() // TODO: Call real use case here -> someUseCase.some()
.map(response -> ResponseEntity.ok().body(response)); // TODO: Customize response here
{{/async}}
{{^async}}
{{#returnType}}
{{>returnTypes}} response = {{operationId}}Mock();
return ResponseEntity.ok().body(response);
{{/returnType}}
{{^returnType}}
return ResponseEntity.ok().build();
{{/returnType}}
{{/async}}
}
{{#async}}
{{#returnType}}
private Mono<{{>returnTypes}}> {{operationId}}Mock() { // TODO: Remove this mock method
{{#examples}}
return Mono.fromSupplier(() -> {
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
try {
return mapper.readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class);
} catch (Exception e) {
throw new RuntimeException("Cannot parse example to {{>returnTypes}}");
}
});
{{/examples}}
{{^examples}}
return Mono.fromSupplier({{>returnTypes}}::new);
{{/examples}}
}
{{/returnType}}
{{^returnType}}
private Mono {{operationId}}Mock() { // TODO: Remove this mock method
return Mono.empty();
}
{{/returnType}}
{{/async}}
{{^async}}
{{#returnType}}
private {{>returnTypes}} {{operationId}}Mock() { // TODO: Remove this mock method
{{#examples}}
try {
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
return mapper.readValue("{{#lambdaRemoveLineBreak}}{{#lambdaEscapeDoubleQuote}}{{{example}}}{{/lambdaEscapeDoubleQuote}}{{/lambdaRemoveLineBreak}}", {{>exampleReturnTypes}}.class);
} catch (Exception e) {
throw new RuntimeException("Cannot parse example to {{>returnTypes}}");
}
{{/examples}}
{{^examples}}
return new {{>returnTypes}}();
{{/examples}}
}
{{/returnType}}
{{/async}}
{{/@first}}
{{/contents}}
{{/operation}}
}
{{/operations}}