All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commons.rest-from-swagger.apiController.mustache Maven / Gradle / Ivy

Go to download

Gradle plugin to create a clean application in Java that already works, It follows our best practices!

There is a newer version: 3.17.22
Show newest version
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}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy