简体   繁体   English

Spring Boot运行正常,但不是它的Jar?

[英]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.

相关问题 如何在命令提示符下运行 spring boot jar 时解决异常“java.lang.IllegalArgumentException: name”但在 eclipse 中运行时很好 - How to solve exception “ java.lang.IllegalArgumentException: name” while running spring boot jar in command prompt but fine while running in eclipse 运行spring boot jar神器的问题 - Problem in running spring boot jar artifact 从 spring 启动 jar 运行 function - Running a function from spring boot jar 运行Jar [Spring Boot]时出现404错误 - 404 error when Running Jar [Spring Boot] 使用 jar 文件运行时出现 Whitelabel 错误,但使用 war 文件以及使用命令 mvn spring-boot:run 运行时工作正常 - Whitelabel error when running with jar file, but works fine with war file and when run with command mvn spring-boot:run 带有Java -jar和Tomcat 8的spring boot getVirtualServerName。STS运行良好 - spring boot getVirtualServerName with java -jar and Tomcat 8. STS runs fine 在运行spring boot jar时使用外部jar作为依赖文件 - Using external jar as dependency file while running spring boot jar 通过-cp而不是-jar运行spring boot application jar - Running spring boot application jar by -cp instead of -jar Spring Boot Jersey - 运行java -jar命令时缺少EmbeddedServletContainerFactory - Spring Boot Jersey - Missing EmbeddedServletContainerFactory when running java -jar command 运行spring boot jar时找不到或加载主类 - Could not find or load main class, when running spring boot jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM