什么是“正确的”?以及保持 Jersey 客户端 API 函数和 REST(Jersey API)服务器函数链接的正确方法?

发布于 2024-11-19 13:52:37 字数 1148 浏览 1 评论 0原文

我想知道拥有更多经验和更复杂项目的人们如何应对 REST 通信中的这种“丑陋”。想象一下以下问题:

我们的 REST 基础设施中的一个特定资源需要相当多的功能,在我的例子中,大约有 50 多个功能,这些功能会导致不同的查询和不同的响应。我试图想出一个有意义的资源树,并将它们分配给可以做“事情”的方法。之后,服务器资源类看起来像这样:

@Path("/thisResource")    
public class SomeResource {

    @GET/POST/PUT/DELETE
    @Path("meaningfulPath")
    public Response resourceFunction1 ( ...lots of Params) {

        ... logic ....

    }

//
// lots of functions ...
//

    @GET/POST/PUT/DELETE
    @Path("meaningfulPath")
    public Response resourceFunctionN ( ...lots of Params) {

    ... logic ....
    }
}

为了构造我的客户端将调用的 url,我做了一个小函数来防止打字错误并更好地使用常量,

所以我的客户端看起来像这样:

public class Client() {
    public returnType function1 () {
        client.resource = ResourceClass.build(Constants.Resouce, "meaningfulPath");
        ...
        return response.getEntity(returnType);
    }

}

现在困扰我的问题是如何我把客户端功能和服务器功能联系起来比较好?

这两块代码之间的唯一联系是由客户端调用并由服务器映射的 URL,如果这个 URL 是在其他地方生成的,这会导致很多混乱。

当我的一位同事需要研究这段代码时,他很难弄清楚 50 多个客户端函数中的哪一个会导致哪个服务器函数。而且很难确定代码中是否存在过时的函数等等。我想你们大多数人都比我更了解不干净代码的问题。

你如何处理这个问题?你如何保持这段代码干净、可维护且美观?

I was wondering how people with more experience and more complex projects get along with this "uglyness" in the REST Communication. Imagine the following Problem:

We'll need a fair amount of functionalities for one specific resource within our REST Infrastructure, in my case that's about 50+ functions that result in different querys and different responses. I tried to think of a meaningful resource-tree and assigned these to methods that will do "stuff". Afterwards, the Server Resource Class looks like this:

@Path("/thisResource")    
public class SomeResource {

    @GET/POST/PUT/DELETE
    @Path("meaningfulPath")
    public Response resourceFunction1 ( ...lots of Params) {

        ... logic ....

    }

//
// lots of functions ...
//

    @GET/POST/PUT/DELETE
    @Path("meaningfulPath")
    public Response resourceFunctionN ( ...lots of Params) {

    ... logic ....
    }
}

To construct the urls my client will call, I made a little function to prevent Typos and to take better use of Constants

so my Client looks like this:

public class Client() {
    public returnType function1 () {
        client.resource = ResourceClass.build(Constants.Resouce, "meaningfulPath");
        ...
        return response.getEntity(returnType);
    }

}

Now the questions that bothers me is how could I link the client function and the server function better?

The only connection between these two blocks of code is the URL that will be called by the client and mapped by the server, and if even this URL is generated somewhere else, this leads to a lot of confusion.

When one of my colleagues needs to get into this code, he has a hard time figuring out which of the 50+ client functions leads to wich server function. Also it is hard to determine if there are obsolete functions in the code, etc. I guess most of you know about the problems of unclean code better than I do.

How do you deal with this? How would you keep this code clean, maintainable and georgeous?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽手叙旧 2024-11-26 13:52:37

通常,这可以通过 EJB 或类似技术来解决。

或者至少通过“真正的”Web 服务,它至少提供 WSDL 和模式 (具有到 Java 接口或“端口”的映射)。

但 REST 通信是非常松散类型和松散结构的。

我现在唯一能想到的是:定义一个项目(让我们称之为“定义”),它将被客户端和服务器引用(因此已知)。在这个项目中,您可以定义一个包含大量 public static final String 的类,例如:

public static final String SOME_METHOD_NAME = "/someMethodName";
public static final String SOME_OTHER_METHOD_NAME = "/someOtherMethodName";

注意:static final String 可以很好地由注释引用(在在这种情况下,编译器认为它是常量)。因此,请使用“常量”来注释您的 @Path,例如:

@Path(Definitions.SOME_METHOD_NAME)

客户端相同:

ResourceClass.build(Constants.Resouce, Definitions.SOME_METHOD_NAME);

Normally, this would be addressed by EJB or similar technologies.

Or at least by "real" web services, which would provide at least WSDL and schemas (with kind of mapping to Java interfaces, or "ports").

But REST communication is very loosely typed and loosely structured.

The only thing I can think of now, is: define a project (let's call it "Definitions") which would be referenced (hence known) by client and server. In this project you could define a class with a lot of public static final String, such as:

public static final String SOME_METHOD_NAME = "/someMethodName";
public static final String SOME_OTHER_METHOD_NAME = "/someOtherMethodName";

Note: a static final String can very well be referenced by an annotation (in that case it is considered to be constant by the compiler). So use the "constants" to annotate your @Path, such as:

@Path(Definitions.SOME_METHOD_NAME)

Same for the client:

ResourceClass.build(Constants.Resouce, Definitions.SOME_METHOD_NAME);
和我恋爱吧 2024-11-26 13:52:37

您错过了 REST 背后的想法。您所做的不是 REST,而是 RPC over HTTP。一般来说,您不应该使用带外知识来构造 URL。相反,您应该跟踪从服务器收到的响应中收到的链接。了解 HATEOAS:

http://en.wikipedia.org/wiki/HATEOAS

You are missing the idea behind REST. What you are doing is not REST but RPC over HTTP. Generally you are not supposed to construct URLs using out of band knowledge. Instead you should be following links received in the responses received from the server. Read about HATEOAS:

http://en.wikipedia.org/wiki/HATEOAS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文