需要做什麼?如何將REST Web服務部署到AWS?
在AWS上部署帶有Web服務(REST)的Web應用程序。 (Tomcat服務器)
我做了什麼?
使用REST Web服務構建Web應用程序。經過本地測試成功。
問題
現在,當我使用彈性魔豆給404錯誤將其部署到AWS。
我的web服務代碼
@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客戶端代碼
<%
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);
}
%>
我的服務器日誌說
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>
檢查你的服務器日誌,看看爲什麼部署失敗 –
@MarkB它只是這個「127.0.0.1說 - - [09 /四月/ 2016:13:38:05 +0000]「POST/SecureStorage/api/abc/crunchifyService HTTP/1.1」404 1070「 – swapnil7
這是您的訪問日誌,而不是您的部署日誌 –