user238021
user238021

Reputation: 1221

Jersey Path resolution

Here is my Jersey Service and I access the service using url

http://host:port/contextroot/welcome/data

When I map the Jersey servlet to /welcome/* - I get an an 404 error. But when I say /* in web.xml, my request goes through fine. I do not want all the requests in my webapp to go through jersey. How do i restrict the path to just requests with /welcome?

@Path("/welcome")
public class WelcomeRestJson {

    @POST
    @Path("/data")
    @Produces("text/plain")
    @Consumes("application/json")

    public String processPostData(MyObject myObject) {
        System.out.println("Inside processPostData");
        return "success";
    }

}

Upvotes: 2

Views: 1442

Answers (2)

Martin Matula
Martin Matula

Reputation: 7989

When mapping the servlet to /welcome/* simply change the path template of the root resource (WelcomeRestJson) from @Path("/welcome") to @Path("/") - that way the same URL (http://host:port/contextroot/welcome/data) will work as before.

Upvotes: 2

Eugenio Cuevas
Eugenio Cuevas

Reputation: 11078

Map to /welcome/* in the web.xml:

<servlet>
    <servlet-name>ServletAdaptor</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/welcome/*</url-pattern>
</servlet-mapping>

Then you can call your webservices under /welcome and the rest of the requests on different paths.

Upvotes: 0

Related Questions