import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; public Response getFile(String contentType) { File f = new File("/tmp/file.doc"); ResponseBuilder response = Response.ok(f); response.type(contentType); response.header("Content-Disposition", "attachment; filename=\"file.doc\""); return response.build(); }
public Response toResponse(final CustomException e) { LOGGER.debug("Mapping CustomException with status + \"" + e.getStatus() + "\" and message: \"" + e.getMessage() + "\""); ResponseBuilder rb = Response.status(e.getStatus()).entity( new ErrorResponseConverter(e.getMessage(), e.getReason(), e.getErrorCode())); List<MediaType> accepts = headers.getAcceptableMediaTypes(); if (accepts!=null && accepts.size() > 0) { //just pick the first one MediaType m = accepts.get(0); LOGGER.debug("Setting response type to " + m); rb = rb.type(m); } else { //if not specified, use the entity type rb = rb.type(headers.getMediaType()); // set the response type to the entity type. } return rb.build(); }
public WebApplicationException createStatusException(String statusMessage) { ResponseBuilder rb = Response.noContent(); rb = rb.type(MediaType.TEXT_PLAIN); rb = rb.status(Status.BAD_REQUEST); rb = rb.entity(statusMessage); return new WebApplicationException(rb.build()); }
@POST @Path("/get") public Response getEmplByPostReqParam(@FormParam("param1") String id) { System.out.println("getEmplByPostReqParam"); EmployeeDAO dbHandler = new EmployeeDAOImpl(); Employee fetchedEmployee = dbHandler.findEmployee(Integer.parseInt(id)); ResponseBuilder rb = new ResponseBuilderImpl(); rb.type(MediaType.APPLICATION_ATOM_XML); rb.entity(fetchedEmployee); return rb.build(); }
builder.type(MediaType.APPLICATION_JSON); Response response = builder.build(); return response;