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

v2.JavaSpring.apiController.mustache Maven / Gradle / Ivy

The newest version!
package {{package}};

{{^jdk8}}
{{#imports}}import {{import}};
{{/imports}}
{{/jdk8}}
{{^isDelegate}}
import com.fasterxml.jackson.databind.ObjectMapper;
{{/isDelegate}}
{{^jdk8}}
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
{{/jdk8}}
import org.springframework.stereotype.Controller;
{{^jdk8}}
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}}
{{/jdk8}}
{{^isDelegate}}
import javax.servlet.http.HttpServletRequest;
    {{#jdk8}}
import java.util.Optional;
    {{/jdk8}}
{{/isDelegate}}
{{^jdk8-no-delegate}}
    {{#useOptional}}
import java.util.Optional;
    {{/useOptional}}
{{/jdk8-no-delegate}}
{{^jdk8}}
    {{^isDelegate}}
import java.io.IOException;
    {{/isDelegate}}
import java.util.List;
    {{#async}}
import java.util.concurrent.Callable;
    {{/async}}
{{/jdk8}}
{{>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;
    }
    {{#jdk8}}

    @Override
    public {{classname}}Delegate getDelegate() {
        return delegate;
    }
    {{/jdk8}}
{{/isDelegate}}
{{^isDelegate}}
    {{^jdk8}}
    private static final Logger log = LoggerFactory.getLogger({{classname}}Controller.class);

    {{/jdk8}}
    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;
    }
    {{#jdk8}}

    @Override
    public Optional getObjectMapper() {
        return Optional.ofNullable(objectMapper);
    }

    @Override
    public Optional getRequest() {
        return Optional.ofNullable(request);
    }
    {{/jdk8}}

{{/isDelegate}}
{{^jdk8}}
{{#operation}}
    public {{#async}}Callable<{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#has this 'more'}},{{/has}}{{/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}}{{#has this 'more'}}, {{/has}}{{/allParams}});
        {{/isDelegate}}
    }

{{/operation}}
{{/jdk8}}
}
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy