Darth.Vader
Darth.Vader

Reputation: 6271

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?

Upvotes: 0

Views: 163

Answers (4)

varun257
varun257

Reputation: 296

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

Upvotes: 0

Yazan
Yazan

Reputation: 6082

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

Upvotes: 1

Vinay Veluri
Vinay Veluri

Reputation: 6855

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

Upvotes: 0

Juned Ahsan
Juned Ahsan

Reputation: 68715

I don't think you need a slash here:

@Path("/{x}")

change this to:

@Path("{x}")

Upvotes: 0

Related Questions