Java JAX-RS/jersey,Spring:管理子资源生命周期

Java JAX-RS/jersey,Spring:管理子资源生命周期,java,spring,rest,jax-rs,Java,Spring,Rest,Jax Rs,我正在使用JAX-RS/Jersey和Spring开发一个RESTful服务。我有两个资源:类别和项目。项是类别的子项。 所以 获取休息/category将返回类别列表。 GET rest/category/123将返回id=123的类别。 获取rest/category/123/item将返回id=123的category中包含的项目列表 以下是我的资源类: @Controller @Path(value = "category") public class CategoryResource

我正在使用JAX-RS/Jersey和Spring开发一个RESTful服务。我有两个资源:类别和项目。项是类别的子项。 所以 获取休息/category将返回类别列表。 GET rest/category/123将返回id=123的类别。 获取rest/category/123/item将返回id=123的category中包含的项目列表

以下是我的资源类:

@Controller
@Path(value = "category")
public class CategoryResource {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<CategoryEntity> getAllCategories() {
        return repo.findAll();
    }

    @GET
    @Path("{id:\\d+}")
    @Produces(MediaType.APPLICATION_JSON)
    public CategoryEntity getCategory(@PathParam("id") long id) {
        return repo.findOne(id);
    }

    @Path("{id:\\d+}/item")
    public ItemResource getItem(@PathParam("id") long id) {
        return new ItemResource(id);
    }


    @Autowired
    private CategoryRepository repo;    
}



public class ItemResource {

    public ItemResource(@PathParam("id") long categoryId) {
        this.category = repoCategory.findOne(categoryId);
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<ItemEntity> getAllItems() {
        return repoItem.findByCategory(this.category);
    }


    private CategoryEntity category;    

    @Autowired
    private CategoryRepository repoCategory; 

    @Autowired
    private ItemRepository repoItem; 

}

是否可以通过在整个会话中保持ItemResource实例的活动状态来防止这种情况,以便在多次请求特定类别的项时重用这些实例?或者这通常是个坏主意?

你的
项目资源是什么?@Cássio Mazzochi Molin对不起,我把事情弄混了一点。我现在已经用一种更清晰的方式重写了我的问题。但子资源定位器的用途是什么呢?我说的是Jersey文档中的示例3.18:我想我误解了你的问题,不理解你的问题。好的,谢谢你。
this.category = repoCategory.findOne(categoryId);