Home > Software Development > Starting with JAX-RS: Configuring javax.ws.rs.ApplicationPath

Starting with JAX-RS: Configuring javax.ws.rs.ApplicationPath

I recently started with a very simple Hello-World-like JAX-RS. So, to start with, I created a class like this:

GenericResource

package com.wordpress.dwuysan;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path(value = "generic")
public class GenericResource {

    @GET
    public String get() {
        return "Hello World";
    }
}

Once the application server is running, when I run curl http://<web application address>/generic, it failed.

After a little bit of Google-ing around, I found that you need to set up the resource path, either by installing a correct servlet on web.xml, or if you are fortunate enough and are using Java EE 6, you can use @javax.ws.rs.ApplicationPath and extends javax.ws.rs.core.Application (Kuchtiak 2009).

So, once I add this class:

ApplicationConfig

package com.wordpress.dwuysan;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath(value = "resources")
public class ApplicationConfig extends Application {
}

I can now invoke curl http://<web application address>/resources/generic, I got correct response.

References:

Kuchtiak, M, 2009, 'How to Combine REST Services with EJB 3.1', accessed 06 December 2012.

About these ads
Categories: Software Development Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 34 other followers