public abstract class AbstractHttpService extends java.lang.Object implements HttpService
HttpService
for easier HTTP service implementation.
This class provides the methods that handles the HTTP requests of the methods their names signify.
For example, doGet()
method handles a
GET
request.
doOptions(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doGet(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doHead(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doPost(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doPut(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doPatch(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doDelete(ServiceRequestContext, HttpRequest, HttpResponseWriter)
doTrace(ServiceRequestContext, HttpRequest, HttpResponseWriter)
405 Method Not Allowed
response
by default. Override one of them to handle requests properly.Constructor and Description |
---|
AbstractHttpService() |
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws java.lang.Exception
HttpRequest
by delegating it to the matching 'doMETHOD()'
method.
Override this method to perform an action for the requests of any HTTP methods:
public class MyHttpService extends AbstractHttpService {
private final Map<HttpMethod, AtomicInteger> handledRequests = new ConcurrentHashMap<>();
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
final HttpResponse res = super.serve(ctx, req);
handledRequests.computeIfAbsent(
req.method(), method -> new AtomicInteger()).incrementAndGet();
return res;
}
}
serve
in interface HttpService
serve
in interface Service<HttpRequest,HttpResponse>
ctx
- the context of the received Request
req
- the received Request
Response
java.lang.Exception
protected void doOptions(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
OPTIONS
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doGet(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
GET
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doHead(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
HEAD
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doPost(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
POST
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doPut(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
PUT
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doPatch(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
PATCH
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doDelete(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
DELETE
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception
protected void doTrace(ServiceRequestContext ctx, HttpRequest req, HttpResponseWriter res) throws java.lang.Exception
TRACE
request.
This method sends a 405 Method Not Allowed
response by default.java.lang.Exception