简体   繁体   中英

Mustache + Spring boot + REST API

I decided to make a project using Mustache + Spring Boot + REST API. Earlier, I used to write projects with the front-end used Java EE + Servlets + JSP. Now I decided to try to switch to Spring and write an application with a front-end. And I found a way to create a page on the front-end using Mustache, but if you use Mustache, then everything needs to be saved to the model , and I want to use REST API. I mean if it is possible? If it's possible I'll be happy if you show a piece of code your RestController and Mustache. The question is 'if possible call endpoint from mustache?'.

Yes, You can create REST API using OpenAPI( https://support.smartbear.com/swaggerhub/docs/tutorials/openapi-3-tutorial.html ) which uses the mustache file to create your code.

You need to add following dependencies,

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.6.8</version>
</dependency>
<dependency>
    <groupId>org.openapitools</groupId>
    <artifactId>jackson-databind-nullable</artifactId>
    <version>0.2.2</version>
</dependency>

add the following plugin in the pom.xml for configuration( https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md ),

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <templateDirectory>${project.basedir}/templates
                </templateDirectory>
                <configOptions>
                    //add the configuration options here
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

By default it uses the mustache files internally to generate codes, you can change the mustache files from the git location( https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/ )/create your own with the name mentioned in git and place it in the location ${project.basedir}/templates it will generate your codes during the compile time and you can use them in your project.

Place the OpenAPI document in the resource folder where application.properties is placed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM