Skip to content
JakeHoward edited this page Jul 16, 2015 · 1 revision

Utterlyidle gives you a way to explicitly build responses without hiding any HTTP details.

public class MyResource {
    @GET
    @Path("/hello")
    public Response hello() {
        return ResponseBuilder.response(Status.OK).header("CustomHeader", "value").entity("entity").build();
    }
}

You can specify a status code, headers and an entity body.

There's no need to specify Content-Length as it will be always calculated by Utterlyidle. If the body is gzipped, the Content-Length header specifies the length of the encoded body, not the length of the original body. What's more the entity body in the resource may be modified by a renderer somewhere down the request handling path, so at the time we construct the response we don't know the value of the final Content-Length.

Streaming Output

If you find yourself need to write output to an output stream, you can use the StreamingOutput interface to get a handle on the response output stream. This is particularly useful for objects that expect to interact with the servlet response API.

e.g. Streaming an XSSFWorkbook via utterlyidle

        return ResponseBuilder
                .response()
                .entity(new StreamingOutput() {
                    @Override
                    public void write(OutputStream outputStream) throws IOException {
                        new XSSFWorkbook().write(outputStream);
                    }
                })
                .header("Content-Disposition", "attachment; filename='excel-is-awesome-honest.xlsx'")
                .build();
Clone this wiki locally