gpt4 book ai didi

java - @PathParam 获取空值

转载 作者:行者123 更新时间:2023-12-01 17:18:10 30 4
gpt4 key购买 nike

即使您刚刚阅读它,也感谢您的帮助。

问题:返回值为null,为什么?检查了很多论坛,一切似乎都很好,但事实并非如此。

package pack.bb;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("path")
public class test {
@GET
@Path("{sorted}")
public String getsortedContactList(@PathParam("list") String list) //throws SQLException
{
System.out.println("GET list: " + list);
return "get " + list;
}
}

我正在使用 localHost 8080

"localHost:8080/testCap/api/path/aaaa"

这是 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Factorial_RESTapp</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>pack.bb</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>

运行后我在控制台中得到这个结果:

获取列表:空

最佳答案

应该是

@Path("/{list}")
public String getsortedContactList(@PathParam("list") String list)

而不是{已排序}。第一个 @Path 标识 url 中的部分,然后 @PathParam 告诉它使用哪个变量,因此它们必须匹配。最后一个,变量名,可以是任何东西。

关于java - @PathParam 获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20599994/

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