1

I created a angular 4 project and using JAVA REST API Project as a BackEnd. I need that I can move my angular 4 project as a staic content inside JAVA war file.

I created the production build for angular project and copy the files from dist folder and paste it inside webcontent of java project. I am able to read the index file of angular project but other files are giving 404.

Please help how can I move my angular project inside my java project to get a single war file for production.

Project Structure::

WEB.XML :-

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>userInformation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Jersey RESTful Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.newgen.ap2</param-value>
    </init-param>
    <init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>com.newgen.ap2.CrossOrigin</param-value>
 </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey RESTful Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping> 
</web-app>
Nishant Varshney
  • 685
  • 2
  • 14
  • 34
  • why would you want to do that? – Stultuske Feb 01 '18 at 09:15
  • @Stultuske so that I have single war for production.any other good approach you are having then please suggest. – Nishant Varshney Feb 01 '18 at 09:18
  • Check this https://stackoverflow.com/questions/45915379/how-to-setup-angular-4-inside-a-java-war-project or this one https://github.com/marco76/java-demo – Saulius Next Feb 01 '18 at 09:20
  • These both are with Maven but i don't want to use maven... – Nishant Varshney Feb 01 '18 at 09:23
  • @NishantVarshney yes, I understand the what, it's the why that baffles me. you are sure you want to change the deployment of your backend each time you change a label in your UI? There's not even a reason they should have to run on the same server, let alone the same war file – Stultuske Feb 01 '18 at 09:33
  • @Stultuske I get your point... But I am having only JBOSS App Server so I am not having any way to deploy the angular 4 project on it. That's why i am going for this approach. – Nishant Varshney Feb 01 '18 at 09:41
  • paste your web.xml and if possible the structure of your webcontent – suenda Feb 01 '18 at 12:02
  • I'd still recommend you read @TimeTraveler answer in the linked article - he explains things you need to do so the angular files are found - regardless of using maven. – JGlass Feb 01 '18 at 13:44
  • Hii Guys.. I done the thing... Now only issue is that I am not able call the background images used in css... It is not taking the image path from root directory.... – Nishant Varshney Feb 02 '18 at 12:41
  • @suenda... I posted what you asked... – Nishant Varshney Feb 02 '18 at 12:51

1 Answers1

0

Change your web.xml to the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>userInformation</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Jersey RESTful Application</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>com.newgen.ap2</param-value>
    </init-param>
    <init-param>
    <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
    <param-value>com.newgen.ap2.CrossOrigin</param-value>
 </init-param>
  </servlet>
  <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
</servlet>
  <servlet-mapping>
    <servlet-name>Jersey RESTful Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping> 

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

The idea is to use a different Servlet to serve your static resources.

Here are some more SO posts that might be helpful:

Servlet filters' order of execution

How are Servlet url mappings in web.xml used?

What happens if I have two servlet mappings in web.xml that match a request?

suenda
  • 773
  • 1
  • 8
  • 21