简体   繁体   中英

Multiple parameters with GET method of JAXRS

I am using java (Netbeans IDE) to create restful services. I have created two entity classes from two related tables. and i have also created the restful services from patterns. i have used Container-Item design pattern.I need to call more than two resources from single Container. i have done the following code

CategoriesResource.java

@Path("/categories")
public class CategoriesResource {

    @Context
    private UriInfo context;

    EjbLookup lookup=new EjbLookup();
    CategoryFacade categoryFacade=lookup.lookupCategoryFacadeBean();

    public CategoriesResource() {
    }

    @GET
    @Produces("application/json")
    public List<Category> getAllCategories() {
        List<Category> categoryList;
        categoryList=categoryFacade.findAll();
        return categoryList;
    }

    @POST
    @Consumes("application/json")
    @Produces("application/json")
    public Response addCategory(Category content) {
        categoryFacade.create(content);
          return Response.created(context.getAbsolutePath()).build();
    }


//    @Path("{category}")
//    public CategoryResource getCategoryResource(@QueryParam("category") String category) {
//        return CategoryResource.getInstance(category);
//    }
    //when I uncomment these four lines of code it gives an exception java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    @Path("{siteID}")
    public CategoryBySiteResource getCategoryBySiteResource(@QueryParam("siteId") Integer site) {
        return CategoryBySiteResource.getInstance(site);
    }

}

CategoryBySiteResource.java

public class CategoryBySiteResource {

    private Integer site;

    EjbLookup lookup=new EjbLookup();
    CategoryFacade categoryFacade=lookup.lookupCategoryFacadeBean();

    private CategoryBySiteResource(Integer site) {
        this.site = site;
    }

    public static CategoryBySiteResource getInstance(Integer site) {
        return new CategoryBySiteResource(site);
    }


    @GET
    @Produces("application/json")
    public List<Category> getCategoryById() {
       return categoryFacade.retrieveCategory(site);
    }

}

CategoryResource.java

public class CategoryResource {

    private String category;

    EjbLookup lookup=new EjbLookup();
    CategoryFacade categoryFacade=lookup.lookupCategoryFacadeBean();

    private CategoryResource(String category) {
        this.category = category;
    }

    public static CategoryResource getInstance(String category) {
        return new CategoryResource(category);
    }


    @GET
    @Produces("application/json")
    public Category getCategoryById() {
       return categoryFacade.find(Integer.parseInt(category));
    }

    @PUT
    @Consumes("application/json")
    public void updateCategory(Category content) {
        categoryFacade.edit(content);
    }

    @DELETE
    public void deleteCategoryById() {


    }
}

Each of your sub-resource locators needs to have a unique path. But you are specifying a path parameter for each that resolves to the same URL. Note that it's irrelevant if you give different names to the path parameters, because the path parameter name is only useful within the context of URL->method parameter mapping. From the URL resolution point of view, they are the same.

Root Relative Path: /categories
Sub Resource Path1: /{category}
Sub Resource Path2: /{siteID}

Given a URL of, for example, http://www.example.com/categories/10 , the runtime cannot determine wether to call sub resource method getCategoryResource or getCategoryBySiteResource , because they are both eligible matches.

In order to solve the problem, you need to give unique path names to each sub-resource method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM