Mono
Mono

Reputation: 241

Java: Jersey rest service URL mapping problem

I´m trying to configure jersey rest services for my java web application. The problem I´m facing is that I can get required response only on root services URL. Any other URL I have tried to set is returning HTTP 404 Not Found.

Following configuration is working:

package cz.mono.wb.rest
@Path("/")
public class UserService 
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String respondAsReady() {
        return "Web service is ready!";
    }
}

When I access URL server:port/contextRoot/rest/ in browse, its returning correct response, but when i try to set the @Path annotation for example like this:

package cz.mono.wb.rest
@Path("/user")
public class UserService 
{
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String respondAsReady() {
        return "Web service is ready!";
    }
}

and try to access URL

server:port/contextRoot/rest/user/

or

server:port/contextRoot/rest/user

its returning 404.

Jersey configuration looks like this: web.xml

<servlet>
<servlet-name>jersey-serlvet</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>cz.mono.wb.rest</param-value>
</init-param>
<init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
</init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

pom.xml

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-server</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.19</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.19</version>
</dependency>

Upvotes: 2

Views: 931

Answers (3)

Massimo
Massimo

Reputation: 1219

Just looked at you code and in an exception it say it has disabled JAR-RS:

 (EJBComponentProviderFactoryInitilizer.java:89) - The EJB interceptor binding API is not available. JAX-RS EJB support is disabled.

Mosto problably solving this problem will solve your issue. Hope I was helpful to solve your issue.

Upvotes: 1

Mustahsan
Mustahsan

Reputation: 3862

As you already specified url-pattern in your config as:

<url-pattern>/rest/*</url-pattern>

Now you dont need to have / before your path in resource. Just try @Path("user") and it should work now.

Upvotes: 1

pavi2410
pavi2410

Reputation: 1285

The path /user is absolute (notice the forward slash / prefixed before user) which means it points to the root of the hostname. Thus, it effectively points to <hostname>/user and not to <hostname>/root/user. So, to fix this just change the value of @Path annotation to "/root/user".

Upvotes: 0

Related Questions