【问题标题】:How to implement maximum 3 try for requesting resource in rest api java如何在rest api java中实现最多3次尝试请求资源
【发布时间】:2023-04-01 11:46:01
【问题描述】:

我想做类似的事情,我应该允许用户请求 3 次。之后只有它应该抛出错误。

我看过很多文章,他们说要使用类似的东西:

for(int i=0;i<3;i++)
{
  try
  {
  }
  catch(Exception e)
  {
    if(count<3)
    {
        count++;
        countinue;
   }
}

我尝试实现这一点,但这仅在一个请求中执行。

我想做的是,对于每个发出的请求,它都应该检查计数。

例如,当我通过邮递员发出一次请求时,它应该检查一个。

当我再次通过邮递员提出请求时,它应该将计数增加到 2,最后第三次它应该抛出错误。

这里是示例代码:

    @PUT
    @Path("/{containerId}/assignnexttask")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response somemethod(@Context HttpHeaders headers, @PathParam("containerId") String containerId,
            @RequestBody String payload) throws JSONException {
        Variant v = RestUtils.getVariant(headers);
        String contentType = RestUtils.getContentType(headers);
        MarshallingFormat format = MarshallingFormat.fromType(contentType);
        // for loop is for maximum 3 attempts ,
        int count=0;
        for (int i = 0; i < 3; i++) {
            try { 
              //here is some code 
            }
            catch(Exception e )
            {
               if(count<3)
               {
                  count++;
                  continue;
               }
             }
         }
     }

希望我能解释一下。在此先感谢。

【问题讨论】:

  • 看起来 count 变量是在类级别声明的。相反,您应该将其重新初始化为方法的第一行。
  • 感谢 Gaurav,它只在方法内部声明。我误删了这里。
  • 你能显示更新的代码吗?为了更好地帮助您?
  • 已更新。请检查
  • 根据当前代码,如果您发出请求并且出现一些错误,它应该这样做,它会在内部尝试 3 次然后抛出错误。虽然您想要的是,如果使用发出请求并且出现一些错误,它应该等待另一个使用请求并继续尝试直到使用 3 个请求,然后抛出错误。这是正确的理解吗?

标签: java jbpm httpconnection rest


【解决方案1】:

您应该更新您的代码,如下所示(示例伪代码)。 (假设如果第一个和第二个请求出现错误,null 应该返回为Response)。

所以你需要维护静态变量。并且在错误成功请求之后或错误抛出之前,变量应该被重新初始化为0,以便为后续请求做好准备。

//class level variable to count the error
private static int count = 0;

    @PUT
        @Path("/{containerId}/assignnexttask")
        @Consumes(MediaType.APPLICATION_JSON)
        @Produces(MediaType.APPLICATION_JSON)
        public Response somemethod(@Context HttpHeaders headers, @PathParam("containerId") String containerId,
                @RequestBody String payload) throws JSONException {
            Variant v = RestUtils.getVariant(headers);
            String contentType = RestUtils.getContentType(headers);
            MarshallingFormat format = MarshallingFormat.fromType(contentType);
            // for loop is for maximum 3 attempts ,
            try { 
                  //here is some code

                   // count = 0 to make it ready for next request if no error
                   count = 0;
                   // Response data
                   return response;
            }
            catch(Exception e ) {
                   if(count<3)
                   {
                      count = count + 1;
                      return null;
                   }
                   // count = 0 to make it ready for next request
                   count = 0;
                   throw e;
             }
        }

【讨论】:

    【解决方案2】:

    像这样?

    for(int i=0;;i++)
    {
      try
      {
         doSomething();
         break;
      }
      catch(Throwable e)
      {
        if(i==2)
        {
           throw e;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 2020-04-30
      相关资源
      最近更新 更多