Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8129989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:31:23+00:00 2026-06-06T08:31:23+00:00

I have a class this is annotated with @Path like so: @Path(widgets) @Produces(MediaType.APPLICATION_XML) public

  • 0

I have a class this is annotated with @Path like so:

@Path("widgets")
@Produces(MediaType.APPLICATION_XML)
public class WidgetResource {

    @GET
    public Response getWidgets(@QueryParam("limit"))
    {
     //This class returns the plural noun, a list of widgets
    //...}

@GET
@Path("widget/{id}")
    public Response getWidgetById(@PathParam("id") long id)
    {
     //This class returns a single widget by id
    //...}

When I fire up a test client the localhost/widgets maps as expected, but when the getWidgetById method is mapped to localhost/widgets/widget/{id}. This is not what I want – I would like to have localhost/widgets and localhost/widget/{id}

I have tried omitting the @Path annotation at the class level, but that prevents Jersey from recognizing this class as a REST Resource (I tried both the ScanningResourceConfig and the ClassNameResourceConfig – both failed to load the class as a resource unless there was a @Path at the class level).

I guess a (ugly) workaround would be to split the methods between classes a WidgetResource class and a WidgetsResource class. I think this is a terrible solution since both of these methods share resources in the same class, but I really need the REST-ful localhost/widget (for a single entity) and localhost/widgets (for plural).

Am I missing something – is there some way for me to have Jersey pick up the class as a Resource class if I just @Path annotate the methods (I couldn’t get it to work), if not can I force absolute mapping (@Path(/widget/{id})) or some relative mapping (@Path(../widget/id) – neither of those work in reality – just an analogy of what I’m after. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:31:24+00:00Added an answer on June 6, 2026 at 8:31 am

    This part is about what you need:

    Personally, I find your mapping strange and confusing. Just keep it like this:

    @Path("widgets")
    @Produces(MediaType.APPLICATION_XML)
    public class WidgetResource {
    
      @GET
      public Response getWidgets(@QueryParam("limit")) {
       //This method returns the plural noun, a list of widgets
       // it's also possible to limit the number returned by
       // using a query parameter. You could easily implement
       // pagination by adding further query parameters like
       // 'offset', 'sortOrder', etc.
       //...
      }
    
      @GET
      @Path("{id}")
      public Response getWidgetById(@PathParam("id") long id) {
        //This method returns a single widget by id
        //...
      }
    }
    

    It seems natural to append the path to a collection with an ID to fetch an object from the collection. There’s really no need to make it widgets/widget/{id}. The widget part is obvious and unnecessary.

    Here’s a really neat tutorial on RESTful APIs: “Teach a dog to REST” by apigee I think it’s a really good video. The authors make a couple of good points. And here’s a link to a longer version of the same presentation


    This part is about what you want:

    If you really want to keep the plural/singular dualism (which I really don’t recomment), you can annotate your code like this:
    But it’s really ugly

    @Path("/")
    @Produces(MediaType.APPLICATION_XML)
    public class WidgetResource {
    
      @GET
      @Path("widgets")
      public Response getWidgets(@QueryParam("limit")) {
       //This method returns the plural noun, a list of widgets
      //...}
    
      @GET
      @Path("widget/{id}")
      public Response getWidgetById(@PathParam("id") long id) {
        //This method returns a single widget by id
        //...
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume I have an annotated bean property setter like this: public class Foo
I have class that looks like this: class A { public: class variables_map vm
I have this class which i would like to map: public class Contract {
I have class like this below shown. which contains the shopping items where the
Lets say we have this JPA-annotated class, with a property of type List. This
I have a class A that holds a class B like this: class A
Situation : I have I class with property annotated with @Autowired: public class MyClass
Say I have classes Foo and Bar set up like this: class Foo {
I have a class that inherits from a base class (this contains a lot
I have class A which extends the Activity class. This class is in package

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.