3

What is need to be done?

Deploy web application with web service(REST) on AWS. (Tomcat server)

What I have done?

Build web application with REST web service. Tested locally successfully.

Problem

Now when I deploy it to the AWS using elastic beanstalk giving 404 error.

My Web Service code

@Path("/abc")
public class registerService {
    @POST
    @Path("/crunchifyService")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response crunchifyREST(InputStream incomingData) {
        StringBuilder crunchifyBuilder = new StringBuilder();
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
            String line = null;
            while ((line = in.readLine()) != null) {
                crunchifyBuilder.append(line);
            }
        } catch (Exception e) {
            System.out.println("Error Parsing: - ");
        }
        System.out.println("Data Received: " + crunchifyBuilder.toString());

        User userObject = new Gson().fromJson(crunchifyBuilder.toString(), User.class);
        System.out.println("JSon object: " + userObject.getSurname());
        int status = RegisterDao.register(userObject);
        if(status == 1)
        {



        }else{
            System.out.println("Failed again");

        }

        // return HTTP response 200 in case of success
        return Response.status(200).entity(crunchifyBuilder.toString()).build();
    }

    @GET
    @Path("/verify")
    @Produces(MediaType.TEXT_PLAIN)
    public Response verifyRESTService(InputStream incomingData) {
        String result = "CrunchifyRESTService Successfully started..";

        // return HTTP response 200 in case of success
        return Response.status(200).entity(result).build();
    }


     @GET
       @Path("/users")
       @Produces(MediaType.TEXT_PLAIN)
       public Response getUsers(){
          return Response.status(200).entity("Done").build();
       }
}

REST client code

<%

 String json = new Gson().toJson(u);
System.out.println(json);

try {
  URL url = new URL("http://storageserver-env.sa-east-1.elasticbeanstalk.com/MyApplicationName/api/abc/crunchifyService");
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setConnectTimeout(5000);
    connection.setReadTimeout(5000);
    OutputStreamWriter out1 = new OutputStreamWriter(connection.getOutputStream());
    out1.write(json);
    out1.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    while (in.readLine() != null) {
    }
    System.out.println("\nCrunchify REST Service Invoked Successfully..");
    in.close();
} catch (Exception e) {
    System.out.println("\nError while calling Crunchify REST Service");
    System.out.println(e);
}

%>

My server log says

127.0.0.1 - - [09/Apr/2016:13:38:05 +0000] "POST /SecureStorage/api/abc/crunchifyService HTTP/1.1" 404 1070

**

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>SecureStorage</display-name>
  <welcome-file-list>
    <welcome-file>
    createTable.jsp</welcome-file>
  </welcome-file-list>

   <servlet>
      <servlet-name>Application</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>registration</param-value>
         </init-param>
      </servlet>
   <servlet-mapping>
   <servlet-name>Application</servlet-name>
      <url-pattern>/api/*</url-pattern>
   </servlet-mapping>
</web-app>
swapnil7
  • 808
  • 1
  • 9
  • 22

1 Answers1

1

There is no difference between deploying web application or web service on AWS.

You will get nice tutorial on aws website about how to deploy web application.

What was my problem?

After calling from rest client , it was giving me 404 error.

How it was solved?.

After instructions in above comments section , I found in my log.

SEVERE [http-nio-8080-exec-8] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet Application java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

This was a actual problem. This exception occurs when having two different versions of the class on your classpath. After this I deleted my extra overlapping jar. More info about above exception

Another problem After above improvement It was expected everything will work fine , but still there 404 error code on service call from rest client.

Solution

My client used url in the following format.

"http://storageserver-env.sa-east-1.elasticbeanstalk.com/MyApplicationName/api/abc/crunchifyService"

URLGivenByAWSWhereWebAppIsDepoyed/MyApplicationName/ResourcePath

Above url format was working on local address i.e. locahost:8080/MyApplicationName/ResourcePath

But it was not working after deployment.

I tried by removing name of MyApplicationName from above url and it worked.

Working format

URLGivenByAWSWhereWebAppIsDepoyed/ResourcePath

Community
  • 1
  • 1
swapnil7
  • 808
  • 1
  • 9
  • 22
  • I am facing the same issue. In my case I get a 204 response but the method "crunchifyREST" is not called . I tried to log the response but it is not hitting that method . Any idea what is the issue? There are no exceptions in the server log – Parth Doshi Sep 27 '16 at 11:40