英文:
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("api")
public class DemoController {
@GetMapping("/demo")
public List<String> demo(){
List<String> list = new ArrayList<>(Arrays.asList("Hello", "World", "from", "Spring!"));
return list;
}
}
The 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;
The 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
Scripts in 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>
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 = { "/", "/{x:[\\w\\-]+}", "/{x:^(?!api$).*$}/*/{y:[\\w\\-]+}","/error" })
public String index() {
return "index.html";
}
}
This code redirects all requests except /api to the index.html file using regex.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论