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

commons.rest-from-swagger.apiExceptionHandler.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}}

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.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
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}}

{{#jakarta}}
import jakarta.servlet.http.HttpServletRequest;
{{/jakarta}}
{{^jakarta}}
import javax.servlet.http.HttpServletRequest;
{{/jakarta}}

import java.util.List;
import java.util.Map;

{{!>generatedAnnotation}}
@RestController
{{#operations}}
public class {{classname}}Controller {
    private static final Logger log = LoggerFactory.getLogger({{classname}}Controller.class);

{{#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}}
        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 Mono<{{>returnTypes}}>(HttpStatus.NOT_IMPLEMENTED);
        {{/async}}
        {{#async}}
        return {{operationId}}Mock()
            .map(response -> ResponseEntity.ok().body(response));
        {{/async}}
    }

    private {{#async}}Mono<{{/async}}{{>returnTypes}}{{#async}}>{{/async}} {{operationId}}Mock() { // please call real usecase
        {{#async}}
        return Mono.fromSupplier({{>returnTypes}}::new);
        {{/async}}
        {{^async}}
        return new {{>returnTypes}}();
        {{/async}}
    }

{{/@first}}
{{/contents}}
{{/operation}}
}
{{/operations}}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy