Search code examples
javaspringazuretomcatazure-webapps

Difference in deploying Java web application and web service on Azure Tomcat


I am having a trouble with deploying on Azure's Tomcat servlet container. I connect to created web-app space with FTP copying the war file to the correct folder.

myWebApp

I have successfully deployed a Spring MVC simple web application on Azure. The URL pattern is below and the both work as expected:

  • http://myWebApp.azurewebsites.net/myWebApp Deployed on Azure Tomcat
  • http://localhost:8080/myWebApp Deployed on local Tomcat

The key file is web.xml below:

<web-app ... xsd schemas ... >

    <display-name>myWebApp</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <context-param>... not important ...</context-param> 
    <listener>... not important ...</listener>
</web-app>

myWebService

Well, the problem comes when I want to deploy a simple web service the same way and see its content.

I have decided to use jersey library. Here is the only sample class:

@Path("Sample")  
public class Sample {
    @GET  
    @Path("/")  
    @Produces(MediaType.TEXT_XML)  
    public String Method() {
        return "<tag>" + 123 + "</tag>";  
    }
}

After clean install and running on local Tomcat server on URL http://localhost:8080/myWebService/Sample, it correctly gives me the XML <tag>123</tag>".

I hopefully deployed on Azure the same way like the previous myWebApp and tried to run on the URL http://myWebService.azurewebsites.net/myWebService. It gaves me the 404 error instead, saying that the requested resource is not available.

Here is the web.xml for sure:

<web-app ... xsd schemas ... >
    <display-name>myWebService</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>nch.webservices</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

What am I missing and I do wrong? I have noticed the difference between both web.xml files in url-pattern that:

  • myWebApp uses /
  • myWebService uses /*

When I use <url-pattern>/</url-pattern> on myWebService, then it either doesn't work on localhost. Thanks for help. Feel free to ask for more info if needed (wanted to make the question as short as possible).


Solution

  • According to your description & codes, I tried to reproduce your issue of the myWebService successfully. The issue 404 of accessing http://myWebService.azurewebsites.net/myWebService of myWebService was caused by the url-pattern not match the Method url of class Sample, please see the explainations below.

    1. In the class Sample, the class annotation @Path("Sample") and the method annotation @Path("/") for method Method means that the accessable url for the method Method of the class Sample in myWebService project is http://myWebService.azurewebsites.net/myWebService/Sample/.
    2. The url-pattern value / means only match the url http://myWebService.azurewebsites.net/myWebService/.

    There are two solutions for your case.

    1. Recommended. Using the wildcard /* instead of / for url-pattern and access the url http://myWebService.azurewebsites.net/myWebService/Sample/. And suggestion that change the class annotation @Path("Sample") to @Path("/Sample") for reading and understanding.

    2. Only change the class annotation @Path("Sample") to @Path("") or @Path("/") if not change url-pattern, but any path annotations for other classes will not be matched.