React路由在Spring Boot Maven项目中在TomEE服务器上无法正常工作。

huangapple go评论76阅读模式
英文:

React routes not working in spring boot maven project on TomEE server

问题

我正在尝试在TomEE服务器上托管一个包含React应用和Spring项目的应用程序。在服务器上,React路由似乎存在一些问题。当在本地运行React应用和Spring应用时,它们可以正常工作,但在服务器上仅根路径(/)可以正常工作。服务器日志中没有错误消息。

这个404错误似乎来自Spring:
404错误消息

以下是控制器类:

@CrossOrigin
@RestController
@RequestMapping("api")
public class DemoController {
    @GetMapping("/demo")
    public List<String> demo(){
        List<String> list = new ArrayList<>(Arrays.asList("Hello", "World", "from", "Spring!"));
        return list;
    }
}

React的App.js:

import {Route, Routes} from "react-router-dom";
import Test from "./components/test";

function App() {
  return (
      <Routes>
          <Route index element={<Test />} />
          <Route path="test" element={<div>Test</div>} />
      </Routes>
  );
}
export default App;

index.js:

import {BrowserRouter} from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
      <BrowserRouter basename={process.env.PUBLIC_URL}>
          <App />
      </BrowserRouter>
  </React.StrictMode>
);

env.local:
PUBLIC_URL=.

env.production:
PUBLIC_URL=/Expo-project-0.0.1-SNAPSHOT

package.json中的脚本:

"scripts": {
    "start": "react-scripts start",
    "build": "env-cmd -f ./.env.production react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
}

pox.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.9</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>no.hvl.dat109</groupId>
    <artifactId>Expo-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>Expo-prosject</name>
    <description>Expo-prosject</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

您已经尝试过在.env文件中添加尾随斜杠,使用哈希路由而不是浏览器路由,并将控制器指向索引文件。但是,问题可能与路由没有始终指向index.html 有关。

英文:

Im trying to host a react app with a spring project on a tomEE server. I'm having some problems with the react routes on the server. It works fine locally when running the react app and sprint app, but only the root path (/) works on the server. There are no error messages in the server logs.

The 404 error seems to be coming from spring:
404 error message

This is the controller class:

@CrossOrigin
@RestController
@RequestMapping(&quot;api&quot;)
public class DemoController {
@GetMapping(&quot;/demo&quot;)
public List&lt;String&gt; demo(){
List&lt;String&gt; list = new ArrayList&lt;&gt;(Arrays.asList(&quot;Hello&quot;, &quot;World&quot;, &quot;from&quot;, &quot;Spring!&quot;));
return list;
}
}

The react App.js:

import {Route, Routes} from &quot;react-router-dom&quot;;
import Test from &quot;./components/test&quot;;
function App() {
return (
&lt;Routes&gt;
&lt;Route index element={&lt;Test /&gt;} /&gt;
&lt;Route path=&quot;test&quot; element={&lt;div&gt;Test&lt;/div&gt;} /&gt;
&lt;/Routes&gt;
);
}
export default App;

The index.js:

...
import {BrowserRouter} from &quot;react-router-dom&quot;;
const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(
&lt;React.StrictMode&gt;
&lt;BrowserRouter basename={process.env.PUBLIC_URL}&gt;
&lt;App /&gt;
&lt;/BrowserRouter&gt;
&lt;/React.StrictMode&gt;
);

env.local:
PUBLIC_URL=.

env.production:
PUBLIC_URL=/Expo-project-0.0.1-SNAPSHOT

Scripts in package.json:

  &quot;scripts&quot;: {
&quot;start&quot;: &quot;react-scripts start&quot;,
&quot;build&quot;: &quot;env-cmd -f ./.env.production react-scripts build&quot;,
&quot;test&quot;: &quot;react-scripts test&quot;,
&quot;eject&quot;: &quot;react-scripts eject&quot;
}

pox.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.7.9&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;no.hvl.dat109&lt;/groupId&gt;
&lt;artifactId&gt;Expo-project&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;war&lt;/packaging&gt;
&lt;name&gt;Expo-prosject&lt;/name&gt;
&lt;description&gt;Expo-prosject&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;11&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;optional&gt;true&lt;/optional&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.postgresql&lt;/groupId&gt;
&lt;artifactId&gt;postgresql&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
&lt;scope&gt;provided&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

I have tried adding a trailing slash in the .env files.
Using hashroutes instead of browserroutes.
Changing the controller to point at the index file. But we might have done something wrong here, because i think the problem is related to the routes not pointing to the index.html at all time.

答案1

得分: 1

我找到了解决方案。正如您所说,404错误是由Spring而不是React引起的。您希望所有路由都指向您的React index.html文件,除了/api。

为了解决这个问题,我创建了一个新的控制器。在这种情况下,我将其命名为Controller.java,并粘贴下面的代码。

@org.springframework.stereotype.Controller
public class Controller {
    @RequestMapping(value = { "/", "/{x:[\\w\\-]+}", "/{x:^(?!api$).*$}/*/{y:[\\w\\-]+}","/error" })
    public String index() {
        return "index.html";
    }
}

这段代码使用正则表达式将所有请求重定向到index.html文件,除了/api。

英文:

I found a solution. As you said the 404 error is caused by spring and not react. You want all routes except /api to point to your react index.html file.

To solve this I created a new controller. In this case i called it Controller.java, and paste the code below.

@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping(value = { &quot;/&quot;, &quot;/{x:[\\w\\-]+}&quot;, &quot;/{x:^(?!api$).*$}/*/{y:[\\w\\-]+}&quot;,&quot;/error&quot;  })
public String index() {
return &quot;index.html&quot;;
}
}

This code redirects all requests except /api to the index.html file using regex.

huangapple
  • 本文由 发表于 2023年3月15日 20:54:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定