rest服务在本地而不是aws上被调用

waxmsbnn  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(168)

我用jersey 2.0restapi创建了一个web应用程序。它在我的本地机器上运行得非常好,但是当我在aws上部署我的应用程序时,restful服务没有被调用。
以下是场景:
下面的服务url在我的本地机器上运行得非常好。我的应用程序的上下文路径是/testrest。
http://localhost:8080/testrest/测试/休息

当部署在aws(tomcat 8 java 8)上时,下面的代码不起作用,并显示404错误:
http://testrest.wm5zqpd6m2.ap-southeast-1.elasticbeanstalk.com/testrest/rest/test
但有趣的是,如果我删除单词testrest并点击下面的url,它将返回一个成功响应。请尝试点击链接并查看结果。我附上pom.xml、web.xml和我的资源类。
http://testrest.wm5zqpd6m2.ap-southeast-1.elasticbeanstalk.com/rest/test
pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.rest</groupId>
  <artifactId>testrest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>testrest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.6</version>
        </dependency>

  </dependencies>
  <build>
    <finalName>testrest</finalName>

    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>

  </build>
</project>

网站.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            com.testing
        </param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>

  </servlet-mapping>
</web-app>

资源类:

package com.testing;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/")
public class TestRest {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/test")
    public Response testRest(){

        return Response.ok("Success Again and Again", MediaType.APPLICATION_JSON).build();
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题