【问题标题】:Deploying a Java Spring, REST and Angular JS app on a standalone Tomcat Server在独立的 Tomcat 服务器上部署 Java Spring、REST 和 Angular JS 应用程序
【发布时间】:2017-08-19 15:30:35
【问题描述】:

我使用 Intellij IDE 创建了一个 Java Spring 和 AngularJS 应用程序。我在 intellij 中创建了一个 war (Web Application:Exploded) 文件。我把这个war文件放在Tomcat webapps目录中,AngularJS代码部署得很好,我可以在浏览器中打开它。当我尝试使用 REST 调用调用我的服务器代码时,我得到了 404 not found 错误,我可以看到 tomcat 目录中没有 java 文件。我复制了java文件,但仍然没有乐趣。 我是部署 Web 应用程序的新手。

如果有人可以帮助我如何让 angularJS 文件正确调用到我的 java 中,将不胜感激。

谢谢。

Spring-Module.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.services.impl"/>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- Creating TransactionManager Bean, since JDBC we are creating of type
      DataSourceTransactionManager -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="getUserRepo" class="com.Repo.impl.UserRepoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="dataManipulationRepo" class="com.Repo.impl.DataManipulationRepoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>


   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1/xx"/>
        <property name="username" value="root"/>
        <property name="password" value="xx"/>
    </bean>
</beans>

mvc-dispatcher-servlet:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.controller"/>

    <mvc:resources mapping="/app/**" location="/app/build/"/>

    <mvc:annotation-driven/>

    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

项目结构

WEB-INF
   |_ jsp folder
   |_ lib folder
   |_ web.xml
   |_ mvc-dispatcher-servlet.xml
   |_ classes folder 
        |_ com folder (controller, model, services, resources, Repo folders with .class files within each)
        |_ Spring-Module.xml                   
app
   |_ html files

解决方案

我搞定了!!我的 REST 调用使用大写字母。我在调用中使用大写的项目名称,然后在导致问题的浏览器 URL 中使用小写。使用 chrome Postman 应用程序也很有帮助。

【问题讨论】:

  • 观察一下,.java 文件是用 java 语言编写的人类可读代码,它不能被 JVM 直接执行,为了让 tomcat 执行你的 java 应用程序,请确保 .class 文件存在(.class 文件是已编译的 java 文件)。在 tomcat 标准部署中,总是有一个 WEB-INF 文件夹尝试验证该文件夹中是否存在 .class 或 .jar 文件。还要验证您的 tomcat 的日志文件夹,以了解部署过程中实际发生的情况。
  • 你的tomcat启动了吗?
  • @DanielC。感谢你的回复。我从我的 intellij 项目中复制了 .class 文件并将它们粘贴到 WEB-INF/classes 目录中。这是正确的方法吗?这是我拥有的相同目录结构。 \tomcat\webapps\LAS\\build\WEB-INF\classes\com\controller - 在控制器文件夹中,我在这里复制了 .Java 和 .class 文件。部署日志没有错误,但是当我进行 REST 调用时出现 404 错误。我的 java 类有一个包:com.controller。任何帮助是极大的赞赏。谢谢。

标签: java angularjs spring tomcat deploying


【解决方案1】:

根据您的回答,您已经使用 IntelliJ 创建了项目的 .war 文件。基于此,请按照以下步骤在 tomcat 中正确部署您的应用程序。以下说明基于linux或mac系统。

  1. 进入你的apache tomcat home,我们将这个目录命名为&lt;tomcat-home&gt;。例如/var/local/apache-tomcat-8/
  2. &lt;tomcat-home&gt; 中,您会找到一些文件夹,例如webappslogbin
  3. 转到webapps 文件夹并将.war 文件放在上面。默认情况下,tomcat 只有在运行时才会尝试部署 .war 文件。如果 tomcat 没有运行,则转到 bin 文件夹并找到一些名为 startup.sh 的文件并执行它(仅适用于 linux 系统),例如 &lt;tomcat-home&gt;/bin/startup.sh。执行startup.sh时会看到为tomcat设置的环境变量,如CATALINA_HOME、JRE_HOME等,如果tomcat无法启动则会显示错误信息。
  4. 要做的第一个评估是查看webapp 文件夹,看看上面是否有一个新文件夹,例如,如果我有myapp.war,那么tomcat 应该创建一个名为myapp 的文件夹,并且在该文件夹内应该存在 WEB-INFMETA-INF 文件夹,这是 Java Web 应用程序的标准。
  5. 查看WEB-INF 文件夹,因为其中应该是classeslib 文件夹,在classes 文件夹中仅存在根据包组织组织在文件夹中的.class 文件。 lib 文件夹中应该存在与您的项目相关的所有 .jar 文件夹,例如 spring、hibernate 等。
  6. 最后查看log 文件夹,里面有一个catalina.out 文件并阅读它以查看任何错误消息。
  7. 在 linux 中查看 tomcat 是否正在运行,您可以通过此命令检查ps -ef | grep java
  8. 为了stop tomcat 转到bin 文件夹并执行shutdown.sh.

要查看如何正确生成 .war 文件,您可以转到此帖子 IntelliJ community can't find Web Application Artifact to generate WAR

【讨论】:

  • 这太棒了。非常感谢。我按照这些步骤,创造了战争,一切都完美地部署了。但是,我仍然在我的服务器休息呼叫中找不到 404。我不知道还有什么可能导致这个问题?我正在使用 spring-mvc-dispatcher 文件进行配置
  • 任何帮助都会很棒。感谢您迄今为止的所有帮助。
  • 能否提供spring配置?还有你项目的文件夹结构。
  • 嗨@Daniel C 非常感谢。我在上面的问题中添加了详细信息。
  • 我搞定了!!非常感谢您的帮助。这一切都奏效了。我的 REST 调用使用大写字母。我在调用中使用大写的项目名称,然后在导致问题的浏览器 URL 中使用小写。使用 chrome Postman 应用程序也很有帮助。再次感谢您,我真的很感激。
猜你喜欢
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 2013-11-04
  • 2020-11-27
  • 2020-07-18
  • 2019-11-13
相关资源
最近更新 更多