【问题标题】:Http method GET shows error when using jersey使用球衣时 Http 方法 GET 显示错误
【发布时间】:2016-08-29 22:18:15
【问题描述】:

我正在学习 REST api 的教程。下面是我的代码,它有 GET 注释并使用 Weblogic 服务器来部署我的应用程序。由于某种原因,它显示以下错误:

HTTP method GET is not supported by this URL

示例.java

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/v1")
public class V1_status {
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String returnTitle(){
        return "<p> Yess </p>";
    }
}

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>com.glasschecker.rest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>Jersey REST Service</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>com.glasschecker.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

  </web-app>

这是教程:

https://www.youtube.com/watch?v=4DY46f-LZ0M&list=PLu47tUtKqNlwfR- nqjiWUaIWOYEi9FyW0&index=2

我使用下面的 URL http://localhost:7001/com.glasschecker.rest/api/v1 将 URL 模式添加到 Servlet 中。

当我尝试访问 http://localhost:7001/com.glasschecker.rest 时,它会正确显示 index.html 文件(我的 web.xml 中的第一个文件)。我确定我的文件中有内容。

注意 java 类在这个包中:

com.glasschecker.rest.status

这是我得到的错误:

weblogic.application.ModuleException: weblogic.management.DeploymentException: [HTTP:101170]The servlet org.foo.rest.MyApplication is referenced in servlet-mapping /api but not defined in web.xml.
    at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)
    at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:237)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:232)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    Truncated. see log file for complete stacktrace
Caused By: weblogic.management.DeploymentException: [HTTP:101170]The servlet org.foo.rest.MyApplication is referenced in servlet-mapping /api but not defined in web.xml.
    at weblogic.servlet.internal.WebAppServletContext.verifyServletMappings(WebAppServletContext.java:1566)
    at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3066)
    at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1830)
    at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:875)
    at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)

【问题讨论】:

  • 你还没有用 @Path 注释你的类
  • 你有一个用@ApplicationPath注解的Application子类吗?也许这就是问题所在?
  • 我假设启动时出现错误?如果这样做,这意味着您收到 404(未找到),因为应用程序未正确部署。
  • 你能发布这个类吗:org.foo.rest.MyApplication?

标签: java rest servlets jersey


【解决方案1】:

这是完整的工作代码:

@Path("/ws")
public class V1_status {

@GET
@Path("/title") //this one is optional
@Produces(MediaType.TEXT_HTML)
public String returnTitle(){
return "<p> Yess </p>";
}

}
  1. 使用注解@Path("path_name_here") 来声明资源的休息路径。
  2. 在 uri 中访问您的端点:http://localhost:8080/ws/title(假设基本服务器路径为:http://localhost:8080
  3. 检查weblogic安装jax-rs-2.0.war
  4. 检查您的 Rest webservice Resource 类是否在包中(使用com.sun.jersey.config.property.package 声明)

【讨论】:

  • 感谢 jeorfevr,但我仍然面临同样的问题
  • 你有没有选择weblogic中安装的jax-rs-2.0.war文件
  • jax-rs-2.0 在 \wlserver\common\deployable-libraries 中找到,所以我希望它已安装。
  • 您可以在您的管理控制台中检查它是否已安装?休闲注册球衣 2.5.1 docs.oracle.com/middleware/1213/wls/RESTF/…
  • 我已经通过管理控制台安装了 jax,但仍然存在同样的问题。我还缺少什么吗?
【解决方案2】:

您定义了/v1 路径但请求/api/v1。 试试:

@Path("/api")
public class V1_status {
    @Path("v1")
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String returnTitle(){
        return "<p> Yess </p>";
    }
}

【讨论】:

  • 感谢现在我收到 Not Found 消息。我使用以下网址:- localhost:7001/com.glasschecker.rest/api/v1/status
  • 没有找到什么?请全部留言。
  • 你的请求必须是 /api/v1 没有状态
  • 当我在没有状态的情况下访问路径时,它会显示一个带有未找到消息的页面。
  • 为什么使用路径localhost:7001/com.glasschecker.rest/api/v1/status,而不是localhost:7001/api/v1?
【解决方案3】:

使用 @Path 注释来注释您的类,因为 JAX-RS 运行时需要将您的类视为资源。

【讨论】:

  • 感谢 Tirath 还是同样的问题
【解决方案4】:

只需将属性“com.sun.jersey.config.property.package”设置为包含 Web 服务类的包。

在您的情况下是“V1_status”类包:“com.glasschecker.rest.status”

<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>com.glasschecker.rest.status</param-value>
</init-param>

Jersey ServletContainer documentation中指定

if the initialization parameter "com.sun.jersey.config.property.resourceConfigClass" is not present and a initialization parameter "com.sun.jersey.config.property.packages" is present a new instance of PackagesResourceConfig is created. The initialization parameter "com.sun.jersey.config.property.packages" MUST be set to provide one or more package names.

PackagesResourceConfig 动态搜索根资源和提供者类。在这种情况下,“V1_status”是您的根资源。

【讨论】:

  • 我认为这可能会解决问题。我在 youtube 视频上看到与此类似的 cmets 来更改 com.sun.jersey.config.property.packages。作者将 设置为 如果您的服务类在不同的包中,这是不正确的。
【解决方案5】:
  1. 删除web.xml文件或清除所有servlet和servlet映射的东西。
  2. 应该有一个扩展ApplicationApplicationConfig 类。注释 ApplicationPath('/') 或任何你想要的。

这是一个示例ApplicationConfig

@javax.ws.rs.ApplicationPath("/")
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(com.testrest.StuffResource.class);
    }

}

【讨论】:

    猜你喜欢
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多