How can I opt NOT to respond to a request in a Java webservice?
Suppose we have a webservice request we don't even want to dignify with a response:
@WebMethod(operationName = "doSomethingForNicePeopleOnly")
public String doSomethingForNicePeopleOnly(@WebParam(name="username") String 开发者_如何转开发user) {
if (userIsNice(user)) {
return "something nice";
} else {
// I'd prefer not to return anything at all, not even a 404...
}
}
What is the best technique for not responding to a given Java webservice request?
Why return nothing at all? That just forces your connection to stay open until it times out, wasting both your server's time and the user's.
I don't know how easy this is to do in Java web services, but can you return 202 ("Accepted" but processing not complete, and may be rejected silently later) or 204 ("No response") maybe?
I think can think of a few choices here:
Return useless response (e.g. empty string):
} else {
return "";
}
Or throw an exception:
} else {
throw new Exception("whatevah....");
}
Or try some delaying tactics followed by #1 or #2:
} else {
Thread.sleep(90000); // 90 seconds!
return "";
}
Also HTTP code 403-Forbidden might be appropriate here as a 4th option.
精彩评论