The JVM SDK currently only supports legacy versions of Nitric prior to v1. This version is maintained for compatibility with existing projects and not recommended for new projects. New projects should be started using a supported SDK (presented automatically using the `nitric new` command) orget in touch to request an update to the latest version.
JVM - api.route.delete()
Register an API route and set a specific HTTP DELETE handler on that route.
import io.nitric.Nitric;
public class Application {
public static void main(String[] args) {
var api = Nitric.INSTANCE.api("public");
api.route("/customers").delete((ctx) -> {
// handle DELETE request
return ctx;
});
Nitric.INSTANCE.run();
}
}
Parameters
- Name
middleware
- Required
- Required
- Type
- HttpMiddleware or List<HttpMiddleware>
- Description
One or more middleware functions to use as the handler for HTTP requests. Handlers can be sync or async
Examples
Register a handler for DELETE requests
import io.nitric.Nitric;
public class Application {
public static void main(String[] args) {
var api = Nitric.INSTANCE.api("public");
api.route("/customers").delete((ctx) -> {
// handle DELETE request
return ctx;
});
Nitric.INSTANCE.run();
}
}
Chain functions as a single method handler
When multiple functions are provided they will be called as a chain. If one succeeds, it will move on to the next. This allows middleware to be composed into more complex handlers.
import io.nitric.Nitric;
import io.nitric.faas.v0.HttpContext;
import io.nitric.faas.v0.Handler;
import java.util.List;
public class Application {
static HttpContext validateRequest(HttpContext ctx, Handler<HttpContext> next) {
// validateRequest
return next.invoke(ctx);
}
static HttpContext handleRequest(HttpContext ctx, Handler<HttpContext> next) {
// handle request
return next.invoke(ctx);
}
public static void main(String[] args) {
var api = Nitric.INSTANCE.api("public");
api.route("/customer").delete(List.of(Application::validateRequest, Application::handleRequest));
Nitric.INSTANCE.run();
}
}
Access the request body
The DELETE request body is accessible from the ctx.req
object.
import io.nitric.Nitric;
public class Application {
public static void main(String[] args) {
var api = Nitric.INSTANCE.api("public");
api.route("/customer").delete((ctx) -> {
var customerData = ctx.getReq().getData();
// parse, validate, and store the request payload...
});
Nitric.INSTANCE.run();
}
}