英文:
How to implement maximum 3 try for requesting resource in rest api java
问题
我想要做的是,我应该允许用户请求3次。之后才应该抛出错误。
我已经阅读了许多文章,它们说要使用类似以下的东西:
for (int i = 0; i < 3; i++) {
try {
}
catch (Exception e) {
if (count < 3) {
count++;
continue;
}
}
}
我尝试实现了这个,但它只在一次请求中执行。
我想要做的是,对于每个请求,它都应该检查计数。
例如,当我通过Postman发送一次请求时,它应该检查一次。
当我再次通过Postman发送请求时,它应该将计数增加到2,最后在第3次时应该抛出错误。
以下是示例代码:
@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循环最多尝试3次,
int count = 0;
for (int i = 0; i < 3; i++) {
try {
// 这里是一些代码
} catch (Exception e) {
if (count < 3) {
count++;
continue;
}
}
}
}
希望我能够解释清楚。提前感谢。
英文:
I want to do something like, I should allow user to request for 3 times. After that only it should throw error.
I have go through many articles, they said to use something like :
for(int i=0;i<3;i++)
{
try
{
}
catch(Exception e)
{
if(count<3)
{
count++;
countinue;
}
}
I tried implementing this, but this executed in just one request.
What I want to do is that for each request made it should check for count.
e.g when I make a request through postman once, it should check for one.
When I again made request through postman it should increase count to 2 and at last for 3rd time it should throw error.
Here is the sample code here :
@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;
}
}
}
}
Hope i am able to explain.Thanks in Advance.
答案1
得分: 1
Here is the translated code snippet:
相反,您应该根据以下方式更新您的代码(示例伪代码)。 (假设如果第一次和第二次请求出错,则应将`Response`返回为`null`)。
因此,您需要维护静态变量。 在错误成功的请求之后或在错误抛出之前,应重新初始化变量为0,以便为后续请求做好准备。
//类级别的变量来计算错误
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循环用于最多3次尝试,
try {
//这里是一些代码
// 将count设置为0,以准备好下一个请求(如果没有错误)
count = 0;
// 响应数据
return response;
}
catch(Exception e ) {
if(count<3)
{
count = count + 1;
return null;
}
// 将count设置为0,以准备好下一个请求
count = 0;
throw e;
}
}
Please note that the code itself is not translated, as requested.
英文:
Rather you should updated your code as below (sample pseudo code). (Assuming that if there is error on first and second request null
should be returned as Response
).
So you need to maintain the static variable. And after error successful request or before error throw, variable should be re-initialized to 0 to make it ready for subsequent requests.
//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
得分: 0
像这样?
for (int i = 0;; i++) {
try {
doSomething();
break;
} catch (Throwable e) {
if (i == 2) {
throw e;
}
}
}
英文:
Like this?
for(int i=0;;i++)
{
try
{
doSomething();
break;
}
catch(Throwable e)
{
if(i==2)
{
throw e;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论