1

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.

Daniel C.
  • 5,418
  • 3
  • 23
  • 26
drea
  • 11
  • 5
  • Just one observation, .java file is the human readable code written in java language it can't be executed by the JVM directly, in order to let tomcat execute your java app make sure that .class files are present (.class files are the compiled java files). In a tomcat standard deployment there is allways a WEB-INF folder try to verify if the .class or .jar files are present in that folder. Also verify the log folder of your tomcat in order to see what is really happening at the deployment process. – Daniel C. Aug 19 '17 at 16:44
  • Did you start your tomcat? – akuma8 Aug 20 '17 at 11:23
  • @DanielC. Thank you for your reply. I copied the .class files from my intellij project and pasted them into the WEB-INF/classes directory. Is this the correct way to do it? This is a same directory structure that I have. \tomcat\webapps\LAS\\build\WEB-INF\classes\com\controller -- inside the controller folder I copied the .Java and .class files in here. The deploy log has no errors however when I make a REST call I get a 404 error. My java classes have a package: com.controller. Any help is greatly appreciated. Thank you. – drea Aug 23 '17 at 20:13

1 Answers1

0

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
  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

Daniel C.
  • 5,418
  • 3
  • 23
  • 26
  • this is excellent. Thank you very much. I followed the steps, created the war and it all deployed perfectly. I am however still getting a 404 not found on my server rest calls. I don't know what else could be causing the issue? I am using a spring-mvc-dispatcher file for configuration – drea Aug 29 '17 at 14:49
  • any help would be great. Thank you for all your help so far. – drea Aug 29 '17 at 14:51
  • could you provide the spring configuration? also the folder structure of you project. – Daniel C. Aug 29 '17 at 17:11
  • Hi @Daniel C thank you so much. I added the details to my question above. – drea Aug 29 '17 at 18:19
  • one question where is /app folder? – Daniel C. Aug 29 '17 at 19:16
  • It's inside > webapps/myProject WEB-INF is in the same directory – drea Aug 29 '17 at 19:27
  • ok just give me a moment to update the answer and give you more information about this error – Daniel C. Aug 29 '17 at 20:59
  • 1
    I got it working!! Thank you very much for your help. This all worked. 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. Thank you again, I really appreciate it. – drea Aug 29 '17 at 21:50
  • good plz share your solution it can help other people with similar error – Daniel C. Aug 29 '17 at 21:56