[英]Spring boot running fine, but not its Jar?
I've build this application using in-memory H2 database. 我已经使用内存中的H2数据库构建了此应用程序。
If I run mvn spring-boot:run it starts Tomcat on port 8081 and I can use the url localhost:8081/Patients just fine and able to interact with the database. 如果我运行mvn spring-boot:run,它将在端口8081上启动Tomcat,并且我可以使用URL localhost:8081 / Patients很好并且能够与数据库进行交互。
If I run mvn package and then java -jar the jar file from /target , it starts Tomcat on port 8081 and the above url returns 404. The message shown is as if it's not finding the resource, which is a jsp file. 如果我运行mvn package ,然后从/ target中对jar文件进行java -jar ,它将在端口8081上启动Tomcat,并且上面的url返回404。显示的消息似乎是找不到资源,它是一个jsp文件。
There was an unexpected error (type=Not Found, status=404). 发生意外错误(类型=未找到,状态= 404)。
/Patients/WEB-INF/views/patient/welcome.jsp /Patients/WEB-INF/views/patient/welcome.jsp
I'm trying to build this application that can live on it's on, but not sure what I'm missing. 我正在尝试构建可以继续使用的应用程序,但不确定我丢失了什么。
pom.xml 的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Paulo</groupId>
<artifactId>Patients</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Patients</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src</directory>
</resource>
</resources>
</build>
</project>
application.properties application.properties
#spring.datasource.url = jdbc:mysql://localhost:3306/pauloaraujo?createDatabaseIfNotExist=true
#spring.datasource.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.username = root
#spring.datasource.password = pauloaraujo
jdbc.driverClassName=org.h2.Driver
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
server.port=8081
server.contextPath=/Patients
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = create
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
EDIT: 编辑:
I learned that Tomcat doesn't hand executable jars with JSPs, then I changed my packaging to WAR. 我了解到Tomcat不会将可执行的jar与JSP交接,然后我将包装改为WAR。
I ran java -jar mywar.war and the result is that Tomcat is on port 8080 and the message is 我运行了java -jar mywar.war ,结果是Tomcat在端口8080上并且消息是
There was an unexpected error (type=Not Found, status=404). 发生意外错误(类型=未找到,状态= 404)。
No message available 无讯息
Check the logs, if you have any, otherwise add logback, so you will have logs. 检查日志(如果有的话),否则添加回发日志,因此您将拥有日志。
Couple of things I noticed: 我注意到的几件事:
remove <scope>provided</scope>
from Tomcat starter and from tomcat-embed-jasper, these are needed with compile scope. 从Tomcat启动程序和tomcat-embed-jasper中删除<scope>provided</scope>
,这些在编译范围内是必需的。
Remove the version number from your Spring dependencies, the parent is there to provide the versions. 从您的Spring依赖项中删除版本号,父级在那里提供版本。
Tomcat is not able to handle jsps in runnable jar . Tomcat无法在可运行的jar中处理jsps 。 Read up on "executable war" (you need war packaging). 阅读“可执行的战争”(您需要战争包装)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.