java - 如何将 REST Web 服务部署到 AWS?

标签 java web-services rest tomcat amazon-web-services

需要做什么?

在 AWS 上使用 Web 服务 (REST) 部署 Web 应用程序。 (Tomcat 服务器)

我做了什么?

使用 REST 网络服务构建网络应用程序。本地测试成功。

问题

现在,当我使用弹性 beantalk 将它部署到 AWS 时,出现 404 错误。

我的网络服务代码

@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>

最佳答案

在 AWS 上部署 Web 应用程序或 Web 服务没有区别。

您将在 aws 网站上获得有关如何部署 Web 应用程序的很好的教程。

我的问题是什么?

从 rest client 调用后,它给了我 404 错误。

它是如何解决的?

按照上面评论部分的说明,我在我的日志中找到了。

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;

这是一个实际问题。当您的类路径中有两个不同版本的类时,会发生此异常。在此之后,我删除了多余的重叠 jar 。 More info about above exception

另一个问题 经过上述改进,预计一切正常,但来自 rest 客户端的服务调用仍然出现 404 错误代码。

解决方案

我的客户使用以下格式的 url。

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

URLGivenByAWSWhereWebAppIsDepoyed/MyApplicationName/ResourcePath

以上 url 格式适用于本地地址,即 localhost:8080/MyApplicationName/ResourcePath

但在部署后它无法正常工作。

我尝试从上面的 url 中删除 MyApplicationName 的名称并且成功了。

工作格式

URLGivenByAWSWhereWebAppIsDepoyed/ResourcePath

关于java - 如何将 REST Web 服务部署到 AWS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36515979/

相关文章:

c# - 判断是数组还是对象

java - 在 Play 1.x 框架中使用 SOAP Web 服务

c# - Web API 获取在 Controller 上进行 UrlEncoded 的请求参数

rest - @RestController 与 StreamingResponseBody async.request-timeout 不工作

java - JPA Hibernate 驼峰字段

java - 客户端服务器端交互问题

version-control - 在 REST 中建模版本控制操作

java - 将 java 对象发送到 rest WebService

java - 在一个 Java 文件中一个接一个地运行程序

java - 为什么我的命令提示符显示未输入任何字符?