简体   繁体   中英

Deploying a Java Spring, REST and Angular JS app on a standalone Tomcat Server

I created a Java Spring and AngularJS application using Intellij IDE. I created a war (Web Application:Exploded) file in intellij. I put this war file in the Tomcat webapps directory and the AngularJS code deployed fine and I could open it in the browser. When I tried to call into my server code using REST calls I got 404 not found errors and I could see that there was no java files in the tomcat directory. I copied over the java files but still no joy. I am new to deploying web applications.

If anyone could please assist me in how I can get the angularJS files to call into my java correctly that would be greatly appreciated.

Thank you.

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>

Project structure

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

Solution

I got it working!! My REST calls were down to capital letters. I was using uppercase for the project name in the call and then lowercase in the browser URL that was causing the issue. Using the chrome Postman app was really helpful too.

According to your answer you have created the .war file of your project using IntelliJ. Based on that follow this steps in order to deploy your app correctly in tomcat. The following instructions are based on a linux or mac system.

  1. Go to your apache tomcat home, we will call this directory as <tomcat-home> . For example /var/local/apache-tomcat-8/
  2. Inside <tomcat-home> you will find some folders like webapps , log , bin .
  3. Go to webapps folder and put .war file on it. By default tomcat will try to deploy the .war file only if it is running. If tomcat is not running then go to bin folder and find some file called startup.sh and execute it (only for linux system) like this <tomcat-home>/bin/startup.sh . When startup.sh is executed you will see the environment variables that are set for tomcat like CATALINA_HOME, JRE_HOME and others, if tomcat can't startup then it will show an error message.
  4. The first evaluation to do is review the webapp folder and see if there is a new folder on it, for example, if I have myapp.war then tomcat should create a folder called myapp and inside that folder should exists a WEB-INF or META-INF folder, that is an standard for java web applications.
  5. Review the WEB-INF folder because inside of it should be a classes and lib folder, in the classes folder exists only the .class files organized in folders depending on the packages organization. In the lib folder should exists all the .jar folders related to your project like spring, hibernate, and many others.
  6. Finally look inside the log folder and inside of it there is a catalina.out file and read it to see any error message.
  7. In linux to see if tomcat is running you can check by this commando ps -ef | grep java ps -ef | grep java
  8. In order to stop tomcat go to bin folder and execute shutdown.sh.

In order to see how to generate the .war file correctly you can go to this post IntelliJ community can't find Web Application Artifact to generate WAR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM