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

JavaSpring.apiController.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
package {{package}};

{{^isJava8or11}}
{{#imports}}import {{import}};
{{/imports}}
{{/isJava8or11}}
{{^isDelegate}}
import com.fasterxml.jackson.databind.ObjectMapper;
{{/isDelegate}}
{{^isJava8or11}}
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
{{/isJava8or11}}
import org.springframework.stereotype.Controller;
{{^isJava8or11}}
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}}
import javax.validation.constraints.*;
import javax.validation.Valid;
    {{/useBeanValidation}}
{{/isJava8or11}}
{{^isDelegate}}
import javax.servlet.http.HttpServletRequest;
    {{#isJava8or11}}
import java.util.Optional;
    {{/isJava8or11}}
{{/isDelegate}}
{{^jdk8-no-delegate}}
    {{#useOptional}}
import java.util.Optional;
    {{/useOptional}}
{{/jdk8-no-delegate}}
{{^isJava8or11}}
    {{^isDelegate}}
import java.io.IOException;
    {{/isDelegate}}
import java.util.List;
    {{#async}}
import java.util.concurrent.Callable;
    {{/async}}
{{/isJava8or11}}
{{>generatedAnnotation}}
@Controller
{{#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}}
    {{^isJava8or11}}
    private static final Logger log = LoggerFactory.getLogger({{classname}}Controller.class);

    {{/isJava8or11}}
    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}}
{{^isJava8or11}}
{{#operation}}
    public {{#async}}Callable<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
        {{^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}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
        {{/isDelegate}}
    }

{{/operation}}
{{/isJava8or11}}
}
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy