如何使用Spring Boot REST API生成的Angular客户端? [英] How to use generated Angular client from Spring Boot REST API?

查看:149
本文介绍了如何使用Spring Boot REST API生成的Angular客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于设法使用本教程,使用openapi-generator-maven-plugin.

I finally managed to generate an Angular client using this tutorial using the openapi-generator-maven-plugin.

我不得不作一些改编,例如使用不同的依赖项

I had to make a few adaptations e.g. using different dependencies

<!-- Spring Docs (Swagger) -->

<!-- TODO After version upgrades check https://github.com/springdoc/springdoc-openapi/issues/133 -->

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-core</artifactId>
    <version>1.1.49</version>
    <exclusions>
        <exclusion>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.1.49</version>
</dependency>

<dependency>
    <groupId>io.github.classgraph</groupId>
    <artifactId>classgraph</artifactId>
    <version>4.8.44</version>
</dependency>

,还必须添加一些其他参数,例如用于数据库驱动程序等:

and also had to add a few more arguments e.g. for the database driver etc.:

<profile>
    <id>angular</id>
    <build>
        <plugins>

            <!-- Code Generation -->

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>reserve-tomcat-port</id>
                        <goals>
                            <goal>reserve-network-port</goal>
                        </goals>
                        <phase>process-resources</phase>
                        <configuration>
                            <portNames>
                                <portName>tomcat.http.port</portName>
                            </portNames>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- Spring Boot Maven plugin -->

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <addResources>true</addResources>
                    <arguments>
                        <!--suppress MavenModelInspection -->
                        <argument>--server.port=${tomcat.http.port}</argument>
                        <argument>--spring.profiles.active=angular</argument>
                    </arguments>
                    <environmentVariables>
                        <JDBC_DATABASE_URL>jdbc:postgresql://localhost/mydatabase</JDBC_DATABASE_URL>
                        <JDBC_DATABASE_USERNAME>postgres</JDBC_DATABASE_USERNAME>
                        <JDBC_DATABASE_PASSWORD>root</JDBC_DATABASE_PASSWORD>
                    </environmentVariables>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>4.2.2</version>
                <executions>
                    <execution>
                        <id>angular-client-code-generation</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <!--suppress MavenModelInspection -->
                            <inputSpec>http://localhost:${tomcat.http.port}/v3/api-docs</inputSpec>
                            <output>${project.build.directory}/generated-sources/angular-client</output>
                            <generatorName>typescript-angular</generatorName>
                            <!--
                                Use this option to dump the configuration help for the specified generator
                                instead of generating sources:
                                <configHelp>true</configHelp>
                            -->
                            <configOptions>
                                <!--
                                    Put generator-specific parameters here, e.g. for typescript-angular:
                                    <apiModulePrefix>Backend</apiModulePrefix>
                                 -->
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>
</profile>

但是现在,在生成代码之后,我无法按照(生成的)README.md中发布的说明进行操作:

But now, after the code has been generated, I am not able to follow the instructions as posted in the (generated) README.md:

@

建筑物

要安装所需的依赖项并构建打字稿 来源运行:

@

Building

To install the required dependencies and to build the typescript sources run:

npm install 
npm run build 

发布

首先构建软件包,然后运行npm publish

publishing

First build the package then run npm publish

导航到消耗项目的文件夹,然后运行下一个 命令.

Navigate to the folder of your consuming project and run one of next commands.

已发布:

npm install @ --save

...

原因:没有package.json,因此也没有可以首先执行的构建"脚本.

The reason: There is no package.json and therefore also no "build" script that could be executed in the first place.

因此,我想知道是否有什么遗漏之处,或者我需要做些什么才能在Angular客户端中使用生成的代码.

Therefore, I am wondering whether there's something missing out or what I have to do to be able to use the generated code in my Angular clients.

推荐答案

我在我必须为openapi-generator-maven-plugin设置<npmName>tmsClientRest</npmName>:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>4.2.2</version>
    <executions>
        <execution>
            <id>angular-client-code-generation</id>
            <phase>integration-test</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!--suppress MavenModelInspection -->
                <inputSpec>http://localhost:${tomcat.http.port}/v3/api-docs</inputSpec>
                <output>${project.build.directory}/generated-sources/angular-client</output>
                <generatorName>typescript-angular</generatorName>                    
                <configOptions>
                    <!-- The missing part(s) -->
                    <npmName>restClientName</npmName>
                    <npmVersion>0.0.1</npmName>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

注意:就我而言,我还必须添加npmVersion使其起作用" (它仍然不起作用,只是遇到下一个问题)

Note: In my case I also had to add npmVersion to make it "work" (it's still not working, just running into the next issue)

更新:

如果您得到类似的东西

models.ts不是模块

这可能是因为您还没有任何DTO对象.就我而言,我只有一个简单的REST控制器,它返回了一个简单的String.只需确保(至少)返回一个虚拟对象,然后用代码填充model.ts,并且npm install && npm run build应该最终可以正常工作,例如:

it might be because you do not have any DTO objects yet. In my case I only had a simple REST Controller that returned a simple String. Just make sure you return (at least) a dummy object and model.ts gets populated with code and npm install && npm run build should finally work for example:

public static class HealthInfoDto {
    public String message = "I am alive, you see?";
}

@RequestMapping(value = "/health", method = RequestMethod.GET)
public @ResponseBody HealthInfoDto getHealth() {
    return new HealthInfoDto();
}

而不是仅仅

@RequestMapping(value = "/health", method = RequestMethod.GET)
public @ResponseBody String getHealth() {
    return "Hello World!";
}

这篇关于如何使用Spring Boot REST API生成的Angular客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆