简体   繁体   English

在 Tomcat 上运行的 Spring Boot API 和 React js

[英]Spring boot API and React js running on Tomcat

I'm developing a React JS application that fetches all data from my Spring boot API.我正在开发一个 React JS 应用程序,它从我的 Spring boot API 中获取所有数据。

Everything works well locally.(Spring boot API on PORT 4000 and React JS on PORT 3000 with a proxy of value "http://localhost/4000")一切都在本地运行良好。(端口 4000 上的 Spring 启动 API 和端口 3000 上的 React JS,代理值为“http://localhost/4000”)

I managed to create a war file that includes both my React js app and Spring boot API.(Running on PORT 8080)我设法创建了一个包含我的 React js 应用程序和 Spring boot API 的 war 文件。(在 PORT 8080 上运行)

I deployed the war file on a local tomcat server with an "ok" status.我将 war 文件部署在本地 tomcat 服务器上,状态为“ok”。

Api calls works very well (http://localhost:8080/webfolder/api/categories for instance) Api 调用效果很好(例如 http://localhost:8080/webfolder/api/categories)

but my React App fails to make api calls correctly (http://localhost:8080/api/categories which it should be http://localhost:8080/webfolder/api/categories) as you can see in the following image:但是我的 React 应用程序无法正确调用 api(http://localhost:8080/api/categories,它应该是 http://localhost:8080/webfolder/api/categories),如下图所示:

这里

this is how my pom.xml look like :这是我的 pom.xml 的样子:

<groupId>com.fox</groupId>
<artifactId>events-platform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>events-platform</name>
<description>Events-platform built using React JS and Spring boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.3.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.9.0</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version><!--$NO-MVN-MAN-VER$-->
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
    <dependency>
        <groupId>jakarta.validation</groupId>
        <artifactId>jakarta.validation-api</artifactId>
        <version>2.0.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-agroal</artifactId>
        <version>5.4.21.Final</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.0.0</version>
        <type>maven-plugin</type>
    </dependency>


</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.github.eirslett</groupId>
            <artifactId>frontend-maven-plugin</artifactId>
            <version>1.6</version>
            <configuration>
                <workingDirectory>${project.basedir}/src/main/webapp/front-end</workingDirectory>
                <installDirectory>target</installDirectory>
            </configuration>
            <executions>
                <execution>
                    <id>install node and npm</id>
                    <goals>
                        <goal>install-node-and-npm</goal>
                    </goals>
                    <configuration>
                        <nodeVersion>v14.5.0</nodeVersion>
                        <npmVersion>6.14.5</npmVersion>
                    </configuration>
                </execution>
                <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>npm run build</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <configuration>
                        <arguments>run build</arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-resources</phase>
                    <configuration>
                        <target>
                            <copy todir="${project.build.directory}/classes/public">
                                <fileset dir="${project.basedir}/src/main/webapp/front-end/build"/>
                            </copy>
                        </target>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And this is how the beginning of my package.json look like :这就是我的 package.json 开头的样子:

包.json

Instance of my API calls in the front-end :我的前端 API 调用实例:

api_call

From what you've shown in your tests, your API is using the /webfolder context, while your ReactJS client is using the root context / .从您在测试中显示的内容来看,您的 API 使用的是/webfolder上下文,而您的 ReactJS 客户端使用的是根上下文/

You could change our client to use the /webfolder context, or configure your API to use the root context / .您可以更改我们的客户端以使用/webfolder上下文,或配置您的 API 以使用根上下文/

UPDATE 2020-10-08 - 12:20更新 2020-10-08 - 12:20

After your update with your web client's sample code, I'd suggest you change your axios calls to something like:使用 Web 客户端的示例代码更新后,我建议您将axios调用更改为:

const API_ROOT = '/webfolder/api'

useEffect(() => {
  axios.get(`${API_ROOT}/spaces`).then(...).catch(...)
  axios.get(`${API_ROOT}/categories`).then(...).catch(...)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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