使用JAXRS的GET方法的多个参数

时间:2013-02-28 03:36:27

标签: java rest jersey jax-rs

我正在使用java(Netbeans IDE)来创建restful服务。我从两个相关的表创建了两个实体类。我也从模式中创造了宁静的服务。我使用了Container-Item设计模式。我需要从单个Container调用两个以上的资源。我已经完成了以下代码

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() {


    }
}

1 个答案:

答案 0 :(得分:3)

每个子资源定位器都需要具有唯一路径。但是您为每个解析为同一URL的路径参数指定了路径参数。请注意,如果为路径参数指定不同的名称,则无关紧要,因为路径参数名称仅在URL-&gt;方法参数映射的上下文中有用。从URL解析的角度来看,它们是相同的。

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

给定一个例如http://www.example.com/categories/10的URL,运行时无法确定调用子资源方法getCategoryResource还是getCategoryBySiteResource,因为它们 都< / em> 符合条件的比赛。

为了解决问题,您需要为每个子资源方法提供唯一的路径名。