简体   繁体   English

球衣与PathParams

[英]Jersey with PathParams

I am trying to pass PathParams in the GET requests to my webservice. 我正在尝试将GET请求中的PathParams传递给我的Web服务。 Here is the Service core: 这是服务核心:

@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";
    }       
}

My web.xml looks like this: 我的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>

When I just call my service like this: 当我这样打电话给我的服务时:

localhost:8080/MyService it returns this works fine as expected. localhost:8080/MyService它将返回this works fine预期的this works fine But when I try passing parameters like this: localhost:8080/MyService/pathParam it throws a 404 . 但是,当我尝试传递这样的参数时: localhost:8080/MyService/pathParam它会抛出404 Any clues? 有什么线索吗?

try not to declare MyService in web.xml, just declare the jersy dispatcher, and in the class declare your service: 尝试不要在web.xml中声明MyService,仅声明jersy调度程序,并在类中声明您的服务:

not tested 未经测试

@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 should not have mapping to your service MyService: should look like this 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>

have a look here for more info 这里看看更多信息

I don't think you need a slash here: 我认为您不需要在这里使用斜线:

@Path("/{x}")

change this to: 更改为:

@Path("{x}")

If you have @Path("/") at class level, I think you dont need at method level any more. 如果您在类级别具有@Path("/") ,我认为您不再需要在方法级别。

Which just makes it like 就像这样

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

Use this : 用这个 :

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

in your web.xml . 在您的web.xml

The calling URL will be /MyService/something/dosomemore 呼叫URL将为/MyService/something/dosomemore

And in your java file, 在您的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