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

commons.rest-from-swagger.apiHandler.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.20.10
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.stereotype.Component;
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;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

{{#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;

{{!>generatedAnnotation}}
{{#lombok}}
@Log4j2
@AllArgsConstructor
{{/lombok}}
{{#operations}}
@Component
public class {{classname}}Handler {
//    private final UseCase someUseCase;
    {{^lombok}}
    private static final Logger log = LoggerFactory.getLogger({{classname}}Handler.class);
    {{/lombok}}

{{#operation}}
{{#contents}}
{{#@first}}
    public Mono {{operationId}}(ServerRequest serverRequest) {
        {{#parameters}}
        {{#isBodyParam}}
        return serverRequest.bodyToMono({{{dataType}}}.class)
                .flatMap(body -> {{operationId}}Mock()) // TODO: Call real use case here -> someUseCase.some()
                .flatMap(response -> ServerResponse.ok().bodyValue(response)); // TODO: Customize response here
        {{/isBodyParam}}
        {{#isFormParam}}
        return serverRequest.bodyToMono(Object.class)
                .flatMap(body -> {{operationId}}Mock()) // TODO: Call real use case here -> someUseCase.some()
                .flatMap(response -> ServerResponse.ok().bodyValue(response)); // TODO: Customize response here
        {{/isFormParam}}
        {{^isBodyParam}}
        {{^isFormParam}}
        return {{operationId}}Mock() // TODO: Call real use case here -> someUseCase.some()
            .flatMap(response -> ServerResponse.ok().bodyValue(response)); // TODO: Customize response here
        {{/isFormParam}}
        {{/isBodyParam}}
        {{/parameters}}
        {{^parameters}}
        return {{operationId}}Mock() // TODO: Call real use case here -> someUseCase.some()
            .flatMap(response -> ServerResponse.ok().bodyValue(response)); // TODO: Customize response here
        {{/parameters}}
    }

    {{#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}}
{{/@first}}
{{/contents}}
{{/operation}}
}
{{/operations}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy