简体   繁体   English

将 REST 查询参数存储为 static 字段

[英]Store REST query parameters as static fields

Is it a good practice to store the query parameters as static fields to handle a request?将查询参数存储为 static 字段来处理请求是一种好习惯吗? For instance, my endpoint accepts two query parameters.例如,我的端点接受两个查询参数。 To validate and process the request body, I have to pass these query parameters to all the low-level methods (like daisy-chaining).要验证和处理请求正文,我必须将这些查询参数传递给所有低级方法(如菊花链)。 To avoid this, I can store these parameters as static fields within a static class and access these parameters whenever required instead of passing it around.为避免这种情况,我可以将这些参数存储为 static class 中的 static 字段,并在需要时访问这些参数,而不是四处传递。 I am fairly new to REST development and I don't want to reinvent the wheel if there are existing patterns to handles the problem I facing.我对 REST 开发相当陌生,如果有现有的模式来处理我面临的问题,我不想重新发明轮子。 Any thoughts on my approach?对我的方法有什么想法吗?

In the code below, I am passing query1, query2, query3 parameters to validateRequest API, which is again passing some of the query parameters to its internal methods.在下面的代码中,我将 query1、query2、query3 参数传递给 validateRequest API,它再次将一些查询参数传递给其内部方法。 This daisy-chaining will continue to low-level methods.这种菊花链将继续用于低级方法。 My question is if store these query parameters in a static class, I don't have to pass the query parameters around.我的问题是如果将这些查询参数存储在 static class 中,我不必传递查询参数。 When an API requires query parameters, it can call the static class to access the parameters.当API需要查询参数时,可以调用static class来访问参数。 Pseudocode:伪代码:

public class Temp {

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)

  public Response post(
      @QueryParam("query1")  final String query1,
      @QueryParam("query2") final String query2,
      @QueryParam("query3") final String query3,
      final Object requestBody) {

    validateRequest(requestBody,query1,query2,query3)

    return Response.status(Status.OK).build();

  }

  private void validateRequest(Object requestBody, String query1, String query2, String query3) {

    validateFirstPartOfRequest(requestBody,query1);
    validateSecondPartOfRequest(requestBody,query1,query2);
    validateThirdPartOfRequest(requestBody,query1),query3;
  }

}

To validate and process the request body, I have to pass these query parameters to all the low-level methods I'm not affraid by this. To validate and process the request body, I have to pass these query parameters to all the low-level methods For instance request parameters will be used for filtering a collection ressource so it sounds logic to pass these parameters until the data access layer for applying your filters at database-level (criteria query).例如,请求参数将用于过滤集合资源,因此在数据访问层用于在数据库级别应用过滤器(条件查询)之前,传递这些参数听起来很符合逻辑。

If you have lot of parameters just wrap them into a parameter object ( https://refactoring.guru/introduce-parameter-object )如果您有很多参数,只需将它们包装成参数 object ( https://refactoring.guru/introduce-parameter-object )

Regarding static it is really a bad idea.关于 static 这真是个坏主意。 Static data will be shared by all requests(threads) coming to your controller and so you'll face some overlap between values. Static 数据将由到达您的 controller 的所有请求(线程)共享,因此您将面临值之间的一些重叠。

It's not recommended to change a static variable from multiple threads just to avoid carrying it in the class instance, but if that's what you want to do, maybe you should have a look at ThreadLocal .不建议从多个线程更改 static 变量,以避免在 class 实例中携带它,但如果这是你想做的,也许你应该看看ThreadLocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM