Class ResponseWriters
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidsendJsonResponse(HttpExchange exchange, int statusCode, Object response) Sends a JSON response using the providedHttpExchange.static voidsendResponse(HttpExchange exchange, int statusCode, String response) Sends a plain text (or any custom format) response using the providedHttpExchange.static voidsse(HttpExchange exchange, int statusCode, StreamingResponse body) static voidstream(HttpExchange exchange, int statusCode, StreamingResponse body)
-
Method Details
-
stream
public static void stream(HttpExchange exchange, int statusCode, StreamingResponse body) throws IOException - Throws:
IOException
-
sse
public static void sse(HttpExchange exchange, int statusCode, StreamingResponse body) throws IOException - Throws:
IOException
-
sendJsonResponse
public static void sendJsonResponse(HttpExchange exchange, int statusCode, Object response) throws IOException Sends a JSON response using the providedHttpExchange.This method sets the
Content-Typeheader toapplication/json, serializes theresponseobject to JSON, and then delegates tosendResponse(HttpExchange, int, String).Note: Using this method directly implies you are manually handling the request and will bypass any post-interceptors. It is recommended that you return
Handled.INSTANCEfrom your request handler to indicate you have fully handled the operation.- Parameters:
exchange- theHttpExchangeto send the response tostatusCode- the HTTP status code to returnresponse- the response object to be serialized as JSON- Throws:
IOException- if an I/O error occurs while writing the response
-
sendResponse
public static void sendResponse(HttpExchange exchange, int statusCode, String response) throws IOException Sends a plain text (or any custom format) response using the providedHttpExchange.This method writes the given
responsestring to the response body and then closes the output stream.Note: Using this method directly implies you are manually handling the request and will bypass any post-interceptors. It is recommended that you return
Handled.INSTANCEfrom your request handler to indicate you have fully handled the operation.- Parameters:
exchange- theHttpExchangeto send the response tostatusCode- the HTTP status code to returnresponse- the response payload as a String- Throws:
IOException- if an I/O error occurs while writing the response
-