简体   繁体   中英

Jersey with PathParams

I am trying to pass PathParams in the GET requests to my webservice. 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:

<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. But when I try passing parameters like this: localhost:8080/MyService/pathParam it throws a 404 . Any clues?

try not to declare MyService in web.xml, just declare the jersy dispatcher, and in the class declare your service:

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

<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.

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 .

The calling URL will be /MyService/something/dosomemore

And in your java file,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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