gpt4 book ai didi

java - Http 方法 GET 在使用 Jersey 时显示错误

转载 作者:搜寻专家 更新时间:2023-11-01 02:39:34 24 4
gpt4 key购买 nike

我正在按照教程学习 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>";
}
}

网络.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("/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 资源类是否在包中(使用 com.sun.jersey.config.property.package 声明)

关于java - Http 方法 GET 在使用 Jersey 时显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37028370/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com