reactjs - 将jar(spring boot 2 + react)部署到heroku时-错误404
问题描述
基于https://blogg.kantega.no/webapp-with-create-react-app-and-spring-boot/制作我自己的项目(https://github.com/770001/buyer ) 。部署到heroku后,应用程序启动时我得到404,但其余控制器不起作用。我不知道为什么。我打了好几天!
PS如果我按照本教程部署应用程序,一切正常。我不明白为什么我有 404。我在另一个教程中尝试了同样的事情 - https://hackernoon.com/package-your-react-app-with-spring-boot-a-how-to-guide-cdfm329w - 如果你将它部署到heroku,一切正常。但是当我创建我的(相同的) - 404。
一切都适用于本地主机。我通过 maven jar 执行,通过运行它,然后通过,java -jar target / <name jar file>
运行反应。一切正常。cd frontend
yarn start
我究竟做错了什么?
更新
我添加到应用程序 Procfile 的根文件夹:
web java -Dserver.port=$PORT $JAVA_OPTS -jar target/shmot-0.0.1-SNAPSHOT.war
这是我的 package.json
{
"name": "front",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.8",
"@testing-library/react": "^11.2.3",
"@testing-library/user-event": "^12.6.0",
"http-proxy-middleware": "^1.0.6",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
这是我的 pom.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.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.khilkevichigor</groupId>
<artifactId>shmot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>shmot</name>
<description>International shopping</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<dependency>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<workingDirectory>front</workingDirectory>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v12.16.3</nodeVersion>
<npmVersion>6.14.4</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}/front/build"/>
</copy>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是来自 heroku 的构建日志:
-----> Building on the Heroku-20 stack
-----> Java app detected
-----> Installing JDK 1.8... done
-----> Executing Maven
$ ./mvnw -DskipTests clean dependency:list install
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.khilkevichigor:shmot >----------------------
[INFO] Building shmot 0.0.1-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ shmot ---
[INFO]
[INFO] --- maven-dependency-plugin:3.1.2:list (default-cli) @ shmot ---
[INFO]
[INFO] --- frontend-maven-plugin:1.6:install-node-and-npm (install node and npm) @ shmot ---
[INFO] Installing node version v12.16.3
[INFO] Unpacking /tmp/codon/tmp/cache/.m2/repository/com/github/eirslett/node/12.16.3/node-12.16.3-linux-x64.tar.gz into /tmp/build_ab545a20_/target/node/tmp
[INFO] Copying node binary from /tmp/build_ab545a20_/target/node/tmp/node-v12.16.3-linux-x64/bin/node to /tmp/build_ab545a20_/target/node/node
[INFO] Installed node locally.
[INFO] Installing npm version 6.14.4
[INFO] Unpacking /tmp/codon/tmp/cache/.m2/repository/com/github/eirslett/npm/6.14.4/npm-6.14.4.tar.gz into /tmp/build_ab545a20_/target/node/node_modules
[INFO] Installed npm locally.
[INFO]
[INFO] --- frontend-maven-plugin:1.6:npm (npm install) @ shmot ---
[INFO] Running 'npm install' in /tmp/build_ab545a20_/front
[INFO]
[INFO] > core-js@2.6.12 postinstall /tmp/build_ab545a20_/front/node_modules/babel-runtime/node_modules/core-js
[INFO] > node -e "try{require('./postinstall')}catch(e){}"
[INFO]
[INFO] Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!
[INFO]
[INFO] The project needs your help! Please consider supporting of core-js on Open Collective or Patreon:
[INFO] > https://opencollective.com/core-js
[INFO] > https://www.patreon.com/zloirock
[INFO]
[INFO] Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)
[INFO]
[INFO]
[INFO] > core-js@3.8.2 postinstall /tmp/build_ab545a20_/front/node_modules/core-js
[INFO] > node -e "try{require('./postinstall')}catch(e){}"
[INFO]
[INFO]
[INFO] > core-js-pure@3.8.2 postinstall /tmp/build_ab545a20_/front/node_modules/core-js-pure
[INFO] > node -e "try{require('./postinstall')}catch(e){}"
[INFO]
[INFO]
[INFO] > ejs@2.7.4 postinstall /tmp/build_ab545a20_/front/node_modules/ejs
[INFO] > node ./postinstall.js
[INFO]
[INFO] Thank you for installing EJS: built with the Jake JavaScript build tool (https://jakejs.com/)
[INFO]
[WARNING] npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules/webpack-dev-server/node_modules/fsevents):
[WARNING] npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
[WARNING] npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules/watchpack-chokidar2/node_modules/fsevents):
[WARNING] npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
[WARNING] npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.1 (node_modules/fsevents):
[WARNING] npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
[ERROR]
[INFO] added 1926 packages from 760 contributors and audited 1929 packages in 45.11s
[INFO]
[INFO] 126 packages are looking for funding
[INFO] run `npm fund` for details
[INFO]
[INFO] found 0 vulnerabilities
[INFO]
[INFO]
[INFO] --- frontend-maven-plugin:1.6:npm (npm run build) @ shmot ---
[INFO] Running 'npm run build' in /tmp/build_ab545a20_/front
[INFO]
[INFO] > front@0.1.0 build /tmp/build_ab545a20_/front
[INFO] > react-scripts build
[INFO]
[INFO] Creating an optimized production build...
[INFO] Compiled successfully.
[INFO]
[INFO] File sizes after gzip:
[INFO]
[INFO] 41.69 KB build/static/js/2.25b2f903.chunk.js
[INFO] 1.39 KB build/static/js/3.5c32b29a.chunk.js
[INFO] 1.16 KB build/static/js/runtime-main.f423ac52.js
[INFO] 802 B build/static/js/main.529d8860.chunk.js
[INFO] 531 B build/static/css/main.8c8b27cf.chunk.css
[INFO]
[INFO] The project was built assuming it is hosted at /.
[INFO] You can control this with the homepage field in your package.json.
[INFO]
[INFO] The build folder is ready to be deployed.
[INFO] You may serve it with a static server:
[INFO]
[INFO] npm install -g serve
[INFO] serve -s build
[INFO]
[INFO] Find out more about deployment here:
[INFO]
[INFO] https://cra.link/deployment
[INFO]
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (default) @ shmot ---
[INFO] Executing tasks
main:
[copy] Copying 19 files to /tmp/build_ab545a20_/target/classes/public
[INFO] Executed tasks
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ shmot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] The encoding used to copy filtered properties files have not been set. This means that the same encoding will be used to copy filtered properties files as when copying other filtered resources. This might not be what you want! Run your build with --debug to see which files might be affected. Read more at https://maven.apache.org/plugins/maven-resources-plugin/examples/filtering-properties-files.html
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ shmot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 15 source files to /tmp/build_ab545a20_/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ shmot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] skip non existing resourceDirectory /tmp/build_ab545a20_/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ shmot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /tmp/build_ab545a20_/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ shmot ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-war-plugin:3.3.1:war (default-war) @ shmot ---
[INFO] Packaging webapp
[INFO] Assembling webapp [shmot] in [/tmp/build_ab545a20_/target/shmot-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Building war: /tmp/build_ab545a20_/target/shmot-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.4.1:repackage (repackage) @ shmot ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ shmot ---
[INFO] Installing /tmp/build_ab545a20_/target/shmot-0.0.1-SNAPSHOT.war to /tmp/codon/tmp/cache/.m2/repository/com/khilkevichigor/shmot/0.0.1-SNAPSHOT/shmot-0.0.1-SNAPSHOT.war
[INFO] Installing /tmp/build_ab545a20_/pom.xml to /tmp/codon/tmp/cache/.m2/repository/com/khilkevichigor/shmot/0.0.1-SNAPSHOT/shmot-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:14 min
[INFO] Finished at: 2021-01-13T07:33:38Z
[INFO] ------------------------------------------------------------------------
-----> Discovering process types
Procfile declares types -> web
-----> Compressing...
Done: 221.8M
-----> Launching...
Released v22
https://shmoting.herokuapp.com/ deployed to Heroku
这是来自 heroku 的应用程序日志:
2021-01-13T07:32:15.000000+00:00 app[api]: Build started by user khilkevichigor@gmail.com
2021-01-13T07:34:01.911760+00:00 app[api]: Deploy 1a70b985 by user khilkevichigor@gmail.com
2021-01-13T07:34:01.911760+00:00 app[api]: Release v22 created by user khilkevichigor@gmail.com
2021-01-13T07:34:03.105197+00:00 heroku[web.1]: State changed from crashed to starting
2021-01-13T07:34:12.000000+00:00 app[api]: Build succeeded
2021-01-13T07:34:24.571031+00:00 heroku[web.1]: Starting process with command `java -Dserver.port=49823 $JAVA_OPTS -jar target/shmot-0.0.1-SNAPSHOT.war`
2021-01-13T07:34:27.415308+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2021-01-13T07:34:27.420758+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8
2021-01-13T07:34:30.011053+00:00 app[web.1]:
2021-01-13T07:34:30.011193+00:00 app[web.1]: . ____ _ __ _ _
2021-01-13T07:34:30.011284+00:00 app[web.1]: /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2021-01-13T07:34:30.011383+00:00 app[web.1]: ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2021-01-13T07:34:30.011477+00:00 app[web.1]: \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2021-01-13T07:34:30.011570+00:00 app[web.1]: ' |____| .__|_| |_|_| |_\__, | / / / /
2021-01-13T07:34:30.011668+00:00 app[web.1]: =========|_|==============|___/=/_/_/_/
2021-01-13T07:34:30.013324+00:00 app[web.1]: :: Spring Boot :: (v2.4.1)
2021-01-13T07:34:30.013382+00:00 app[web.1]:
2021-01-13T07:34:30.433585+00:00 app[web.1]: 2021-01-13 07:34:30.428 INFO 4 --- [ main] c.khilkevichigor.shmot.ShmotApplication : Starting ShmotApplication v0.0.1-SNAPSHOT using Java 1.8.0_275-heroku on affa4dd6-bc17-41aa-b83b-7290ef507210 with PID 4 (/app/target/shmot-0.0.1-SNAPSHOT.war started by u5738 in /app)
2021-01-13T07:34:30.436511+00:00 app[web.1]: 2021-01-13 07:34:30.436 INFO 4 --- [ main] c.khilkevichigor.shmot.ShmotApplication : No active profile set, falling back to default profiles: default
2021-01-13T07:34:34.414783+00:00 app[web.1]: 2021-01-13 07:34:34.414 INFO 4 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-01-13T07:34:34.670946+00:00 app[web.1]: 2021-01-13 07:34:34.670 INFO 4 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 223 ms. Found 2 JPA repository interfaces.
2021-01-13T07:34:37.245660+00:00 app[web.1]: 2021-01-13 07:34:37.244 INFO 4 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 49823 (http)
2021-01-13T07:34:37.288439+00:00 app[web.1]: 2021-01-13 07:34:37.288 INFO 4 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-01-13T07:34:37.288893+00:00 app[web.1]: 2021-01-13 07:34:37.288 INFO 4 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2021-01-13T07:34:39.683441+00:00 app[web.1]: 2021-01-13 07:34:39.683 INFO 4 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-01-13T07:34:39.683661+00:00 app[web.1]: 2021-01-13 07:34:39.683 INFO 4 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 8934 ms
2021-01-13T07:34:40.337172+00:00 app[web.1]: 2021-01-13 07:34:40.334 INFO 4 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-13T07:34:40.486708+00:00 app[web.1]: 2021-01-13 07:34:40.485 INFO 4 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.25.Final
2021-01-13T07:34:41.135637+00:00 app[web.1]: 2021-01-13 07:34:41.135 INFO 4 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-01-13T07:34:41.407961+00:00 app[web.1]: 2021-01-13 07:34:41.407 INFO 4 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-01-13T07:34:42.079667+00:00 app[web.1]: 2021-01-13 07:34:42.079 INFO 4 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-01-13T07:34:42.171030+00:00 app[web.1]: 2021-01-13 07:34:42.170 INFO 4 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2021-01-13T07:34:43.969250+00:00 app[web.1]: 2021-01-13 07:34:43.968 INFO 4 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-01-13T07:34:43.987686+00:00 app[web.1]: 2021-01-13 07:34:43.987 INFO 4 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-01-13T07:34:45.065217+00:00 app[web.1]: 2021-01-13 07:34:45.064 WARN 4 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-01-13T07:34:47.509625+00:00 app[web.1]: 2021-01-13 07:34:47.507 INFO 4 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@235a0c16, org.springframework.security.web.context.SecurityContextPersistenceFilter@3401a114, org.springframework.security.web.header.HeaderWriterFilter@73173f63, org.springframework.security.web.authentication.logout.LogoutFilter@319bc845, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@5e8ac0e1, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4233e892, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@58ffcbd7, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2b5f4d54, org.springframework.security.web.session.SessionManagementFilter@655ef322, org.springframework.security.web.access.ExceptionTranslationFilter@25e2ab5a, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@37ddb69a]
2021-01-13T07:34:47.944922+00:00 app[web.1]: 2021-01-13 07:34:47.944 INFO 4 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-01-13T07:34:48.163350+00:00 app[web.1]: 2021-01-13 07:34:48.163 INFO 4 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page: class path resource [public/index.html]
2021-01-13T07:34:48.933082+00:00 heroku[web.1]: State changed from starting to up
2021-01-13T07:34:48.717965+00:00 app[web.1]: 2021-01-13 07:34:48.716 INFO 4 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 49823 (http) with context path ''
2021-01-13T07:34:48.745993+00:00 app[web.1]: 2021-01-13 07:34:48.745 INFO 4 --- [ main] c.khilkevichigor.shmot.ShmotApplication : Started ShmotApplication in 19.827 seconds (JVM running for 21.325)
感觉 React 不行了!
解决方案
推荐阅读
- javascript - 如何使用 mocha/sinon/chai 正确测试异步流错误事件处理?
- c++ - 在 OpenGL 中使用和绑定多个 vbo
- apache-flink - 如何检测 Flink Batch Job 是否完成
- sql - 具有多个变量的sql日期差异
- visual-studio - 在哪里可以找到 Visual Studio Team Explorer 错误详细信息?
- kdb - KDB+:将 2 阶段查询转换为单阶段查询的有效方法?
- powershell - 如何提供 PowerShell 参数的自定义类型转换
- ruby-on-rails - Rails 将临时属性合并到模型对象
- c++ - 如何解决苹果设备上的“clang:错误:不支持的选项“-fsanitize=leak”
- javascript - 我制作的这个简单的待办事项列表应用程序有什么问题?