我正在使用 java (Netbeans IDE) 创建宁静的服务。我从两个相关的表中创建了两个实体类。而且我还从模式创建了宁静的服务。我使用了 Container-Item 设计模式。我需要从单个 Container 调用两个以上的资源。我已经完成了以下代码
分类资源.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);
}
}
类别资源.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() {
}
}