RESTful produce el archivo binario
Soy nuevo usando CXF y Spring para hacer servicios web RESTful.
Este es mi problema: quiero crear un servicio que produzca "cualquier" tipo de archivo (puede ser imagen, documento, txt o incluso pdf), y también un XML. Hasta ahora obtuve este código:
@Path("/download/")
@GET
@Produces({"application/*"})
public CustomXML getFile() throws Exception;
No sé exactamente por dónde empezar, así que sea paciente.
EDITAR
Código completo de Bryant Luk (¡gracias!)
@Path("/download/")
@GET
public javax.ws.rs.core.Response getFile() throws Exception {
if (/* want the pdf file */) {
File file = new File("...");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("content-disposition", "attachment; filename =" + file.getName())
.build();
}
/* default to xml file */
return Response.ok(new FileInputStream("custom.xml")).type("application/xml").build();
}