0

I am using Eclipse to develop a Java project to run on a server. I have two projects:

1) jbosswildfly: one with Java code made up of a number of RESTful Services, and a Maven pom.

2) theWhoZoo-web: the other is a web project containing a few html files.

enter image description here

I would like to merge these projects and just have one project. I have tried copying the WebContent folder from theWhoZoo-web to jbosswildfly, starting the server, but I cannot access the index.html.

Question

What is the best way to merge these two projects, so that the RESTful Services as well as the index.html are accessible on the same server?

Thanks

UPDATE

I try run the JBoss index.html but get a 404.

enter image description here

But, when I invoke one of the RESTful Services, it returns a result.

e.g. http://localhost:8080/jbosswildfly-1.0/category/list

but,

http://localhost:8080/jbosswildfly-1.0/index.html

returns:

14:23:31,699 WARN [org.springframework.web.servlet.PageNotFound] (default task-4) No mapping found for HTTP request with URI [/jbosswildfly-1.0/index.html] in DispatcherServlet with name 'rest'

My pom.xml has:

                <plugin>
                    <groupId>org.wildfly.plugins</groupId>
                    <artifactId>wildfly-maven-plugin</artifactId>
                    <version>1.0.2.Final</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <outputDirectory>webapps</outputDirectory>
                        <warName>ROOT</warName>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                    <configuration>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <outputDirectory>deployments</outputDirectory>
                        <warName>ROOT</warName>
                    </configuration>
                </plugin>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.1"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         metadata-complete="false">
   <!--       
         <servlet>
            <servlet-name>rest</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
         </servlet>
  -->        
         <servlet-mapping>
            <servlet-name>rest</servlet-name>
            <url-pattern>/*</url-pattern>
         </servlet-mapping>

</web-app>

WebAppInitializer.java

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(servletContext);
        Dynamic dynamic = servletContext.addServlet("rest", new DispatcherServlet(ctx));
        dynamic.addMapping("/*");
        dynamic.setLoadOnStartup(1);
}
Richard
  • 8,193
  • 28
  • 107
  • 228

2 Answers2

1

You must move files under WebContent folder from theWhoZoo-web to jbosswildfly's src/main/webapp as maven default folder for web resources, if not a such, you must create it.

In case if you want to keep the WebContent as your static web files directory, you can configure pom.xml like:

...
<build>
<plugins>
<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
</plugin>
....

To make DispatchServlet ignore the .html, you can

 <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/**/*.do</url-pattern>
 </servlet-mapping>

In such case, spring-mvc well only intercept the *.do requests, and the .html requests well bypass to the container.

If there are many links in your spring project, maybe change the .html extension to .jsp is more easy.

If .do is not enough, then you can add more like

  <servlet-mapping>
        <servlet-name>rest</servlet-name>
        <url-pattern>/**/*.do</url-pattern>
         <url-pattern>/category/*</url-pattern>
        <url-pattern>/somethingelse/*</url-pattern>
 </servlet-mapping>
Yu Jiaao
  • 4,444
  • 5
  • 44
  • 57
  • Thanks, I have tried that. There was already an `index.html`, see UPDATE above. I get a 404 unfortunately. I can access the RESTful Service though. Do you think there is some config issue? – Richard May 31 '17 at 12:00
  • It looks you use `spring-mvc`, then you can: create a controller use `index.html` as a template, or change `org.springframework.web.servlet.DispatcherServlet` servlet mapping in your `web.xml`, so that DispatcherServlet can bypass the `.html` extension and the container can serve it directly. – Yu Jiaao May 31 '17 at 12:34
  • Apologies for my ignorance, but yes I am using Spring to framework the RESTful Services. In order to cater for the `index.html`, I have added a `` to the `web.xml` file (see above). But unfortunately there is no change. Is that what you mean? – Richard May 31 '17 at 12:38
  • I think it is actually using the `WebAppInitializer.java`. – Richard May 31 '17 at 12:44
  • adding `/**/*.do` worked, thank you. I can now access the `index.html`. However, no I cannot access the RESTful Services, e.g. `http://localhost:8080/jbosswildfly-1.0/category/list` returns a `404`. – Richard May 31 '17 at 12:48
  • look at this https://stackoverflow.com/a/4140659/1484621 – Yu Jiaao May 31 '17 at 12:53
  • Thanks. I will have a look. Just had to step away from my pc for a bit, will try figure it out when I get back. I guess though that I would need two 's for both static html pages and the RESTful Services. – Richard May 31 '17 at 13:04
  • Thanks, I see your update says I should try: `/category/*`, I do but this still does not allow: `http://localhost:8080/jbosswildfly-1.0/category/list`. any ideas? p.s. `/*` or `/` does allow the RESTful Service, but blocks the html. – Richard May 31 '17 at 13:28
  • The following allows the Service and the html. `dynamic.addMapping("/**/*.do");` and `dynamic.addMapping("/category/list");`. But `/category/*` does not work. – Richard May 31 '17 at 13:33
0

Well it isn't going to be simple.

As a general guide, you may want to start by copying the source code and tests from one to the other, make sure it still builds and add any missing dependencies. (My inclination would be to try moving jbosswildfly to theWho-soo first)

Then add the resources including the web directories from one to the other. Edit your index.html and web.xml files to reflect the new structure.

Launch the web server and check the logs for any exceptions. Try and fix them yourself and if you can't then come back to stackoverflow.

Sam
  • 670
  • 1
  • 6
  • 20