You
can configure exceptions using Spark.
public class IdMissingException extends Exception{ IdMissingException(){ super("Id is missing in request parameter"); } }
import static spark.Spark.*; public class HelloSpark { public static void main(String[] args) { get("/getDetails", (request, response) -> { String id = request.queryParams("id"); if(id == null) throw new IdMissingException(); return "Hello" + id; }); exception(IdMissingException.class, (e, request, response) -> { response.status(404); response.body("Resource not found"); }); } }
Response
Headers like below.
Status Code: 404 Not Found
Content-Length: 18
Content-Type: text/html; charset=UTF-8
Server: Jetty(9.0.2.v20130417)
Response
body like below.
Resource not found
No comments:
Post a Comment