繁体   English   中英

球衣与PathParams

[英]Jersey with PathParams

我正在尝试将GET请求中的PathParams传递给我的Web服务。 这是服务核心:

@Path("/")
public class MyService {

    @GET
    @Produces
    public String getIntentClassIds() {
        return "this works fine";
    }

    @GET
    @Path("/{x}")
    @Produces
    public String getIntentClassById(@PathParam("x") String intentClassId) {
        return "This does not work";
    }       
}

我的web.xml看起来像这样:

<servlet>
    <servlet-name>MyService API</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.mypackagename</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyService API</servlet-name>
    <url-pattern>/MyService</url-pattern>
</servlet-mapping>

当我这样打电话给我的服务时:

localhost:8080/MyService它将返回this works fine预期的this works fine 但是,当我尝试传递这样的参数时: localhost:8080/MyService/pathParam它会抛出404 有什么线索吗?

尝试不要在web.xml中声明MyService,仅声明jersy调度程序,并在类中声明您的服务:

未经测试

@Path("/MyService")
public class MyService {

    @GET
    @Produces
    @path("getIntentClassIds")
    public String getIntentClassIds() {
        return "this works fine";
    }

    @GET
    @Path("getIntentClassById/{x}")
    @Produces
    public String getIntentClassById(@PathParam("x") String intentClassId) {
        return "This does not work";
    }

}  

web.xml不应映射到您的服务MyService:应如下所示

<servlet>
    <servlet-name>MyService API</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.mypackagename</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>MyService API</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

这里看看更多信息

我认为您不需要在这里使用斜线:

@Path("/{x}")

更改为:

@Path("{x}")

如果您在类级别具有@Path("/") ,我认为您不再需要在方法级别。

就像这样

localhost:8080/MyService/(/ -> this is at service class level)[If you keep another here ; I think it cannot parse]pathParam

用这个 :

<url-pattern>/MyService/*</url-pattern>

在您的web.xml

呼叫URL将为/MyService/something/dosomemore

在您的Java文件中

@Path("/something")
public class MyService {

    @GET
    @Produces
    public String getIntentClassIds() {
        return "this works fine";
    }

    @GET
    @Path("/dosomemore")
    @Produces
    public String getIntentClassById(@PathParam("x") String intentClassId) {
        return "This does not work";
    }       
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM