Kikkomann
Kikkomann

Reputation: 416

Deploying a REST API to AWS

I am making a distributed system as a school project and I need to have a REST service. This will be a simple service with a login/register function and some information transfer.

I have made the REST API in Java in NetBeans. It works fine locally, but I am having difficulties to put it on my AWS server. I have no experience with servers, so I don't really know how it works. I thought that it should easy to get the service up and running on a server.

So far I have used this guide for the REST and tried to deploy the war-file with Elastic Beanstalk.

My Java code:

ApplicationConfig.java

package dk.dtu.ds.login;

import java.util.Set;
import javax.ws.rs.core.Application;

@javax.ws.rs.ApplicationPath("CoL")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        addRestResourceClasses(resources);
        return resources;
    }

    /**
     * Do not modify addRestResourceClasses() method.
     * It is automatically populated with
     * all resources defined in the project.
     * If required, comment out calling this method in getClasses().
     */
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(dk.dtu.ds.login.Login.class);
    }
}

Login.java

package dk.dtu.ds.login;

import cleanoutloudserver.ICleanOutLoud;
import java.net.MalformedURLException;
import java.net.URL;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

@Path("login")
public class Login {
    // HTTP Get Method

    @GET
    @Path("dologin")
    // Produces JSON as response
    @Produces(MediaType.APPLICATION_JSON)
    // Query parameters are parameters: http://localhost/colrest/CoL/login/dologin?username=s150157&password=1234
    public String doLogin(@QueryParam("username") String uname, @QueryParam("password") String pwd) throws MalformedURLException, Exception {
        URL url = new URL("http://ec2-52-43-233-138.us-west-2.compute.amazonaws.com:3769/col?wsdl");
        QName qname = new QName("http://cleanoutloudserver/", "CleanOutLoudImplService");
        Service service = Service.create(url, qname);
        ICleanOutLoud col = service.getPort(ICleanOutLoud.class);

        String token = col.login(uname, pwd);

            token = Utility.constructJSON(token);
            System.out.println("\nChecking credentials = true\n");
        return token;
    }
}

web.xml

<web-app 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"
         version="3.1">
    <display-name>RESTWebApp</display-name>
    <servlet>
        <servlet-name>jersey-servlet</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>dk.dtu.ds.login</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

When I then try to open the path for the service, I get a blank page. My Chrome console says "GET (link) 404 (Not Found)"

Since I am not that familiar with HTTP and servers, I don't know what to do. Isn't there an easy way to deploy a simple REST service with AWS or have I done something wrong?

I have really tried to search google to find help, but there has been no success so far.

Upvotes: 2

Views: 1115

Answers (1)

Kikkomann
Kikkomann

Reputation: 416

It seems like i got too confused by all the guides out there. I found an easy solution and installed Tomcat on the EC2 instance, so I didn't even need to use Beanstalk.

All I did was following this guide and uploaded the war-file in the Web Application Manager and now it works fine.

Thanks for the comments they helped me on the way to find a solution.

Upvotes: 1

Related Questions