English 中文(简体)
利用继承和一般财产来组织泽西岛资源的适当方式是什么?
原标题:What is the right way to organize Jersey resources using inheritance and generics?

我会同泽西岛开发一个我拥有许多资源的 app。 虽然这些资源的主要功能各不相同,但它们分享了许多共同方法(如清单、阅读、更新等)。 这些仪器在谷歌应用引擎上运行,并利用Guice进行依赖注射。

My first approach was to have a generic AbstactResource which contains all common logic, and it s respectively extended by all other resources which add their required custom methods.

public class AbstractResource<T> {

@GET
public ListPage<T> list(@QueryParam("limit") Integer limit,
    @QueryParam("start") Integer start) {
    // ... implementation
}

@GET
@Path("/{id}")
public T get(@PathParam("id") Long id) {
    // ... implementation
}

And sample resource looks like:

public class TenantResource extends AbstractResource<Tenant> {
    // custom resource related methods here
}

在该案中,所有工作都属于罚款。 在我增加一个层次的抽象性时,问题就会出现。 我要说的是,我只想为我的一些资源储存历史和变幻灯。 I ve还创建了一个更抽象的班级,将《摘要检索》称为“法院可核查资源”,增加了必要的功能。

public abstract class AuditableResource<T extends AuditableModel> 
    extends AbstractResource {
        // here I override update and create methods to save changelogs
}

如你认为,本案中的类型参数已经改变(现在可适用Model)。

新的具体资源将考虑:

public class PropertyResource extends AuditableResource<Tenant> {
    // custom resource related methods here
}

在此情况下,一切都仍然奏效,但此时此刻,我收到许多关于启动的警告信息:

WARNING: Return type T of method public T com.pkg.AbstractResource.get(java.lang.Long) is not resolvable to a concrete type
WARNING: Return type T of method public T com.pkg.AbstractResource.getNew() is not resolvable to a concrete type
WARNING: Return type com.pkg.data.ListPage<T> of method public com.pkg.ListPage<T> com.pkg.AbstractResource.list(java.lang.Integer,java.lang.Integer) is not resolvable to a concrete type

我真的想知道,这一办法是否正确使用泽西岛,我是否能够仅仅忽视这一信息。 她很想知道,如果资源很多,如何安排资源。

最佳回答

一种办法是将资源的定义与执行分开。

  • Have very simple resource classes, defining the different services you want to offer. This way, the API you expose through rest is easily located and audited. The different methods are probably delegates to an implementation class
  • Implement the business logic of your resources in the implementations, where you might want to use inheritance to factor common behavior.

您在操作时获得这些信息的原因是,妇女委员会利用了资源类型方面的实时信息。 汇编时删除的一般类型信息无法得到通用类别方法的实际回报类型。 如果你为你的执行提供教育、科学和技术咨询,你可以表明这一点。

public class Facade {
  private final PropertyResource propertyResource;
  public Facade() {
    propertyResource = new PropertyResource();
  }
  @GET
  @Path("somepath")
  public Tenant something() {
    return propertyResource.something();
  }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签