allora, usare l'estensione non mi piace un granchè.
sto provando a passare il tipo direttamente nel path.
questa cosa qui funziona, però vorrei migliorare il codice:
se gli passo come ultimo argomento xls, mi scarica il file (vuoto al momento, me è l'ultimo dei problemi).codice:@Path("test") public class TestService { @GET @Path("{mediaType}") public Response test(@PathParam("mediaType") String mediaType) { ZonaQueries query = new ZonaQueries(); ResponseBuilder response; ArrayList<Zona> list = null; try { list = query.getAll(); } catch (ClassNotFoundException | SQLException | MappableException ex) { throw new NotFoundException(new JsonError("Errore", ex.getMessage())); } if (mediaType.equals("json")) { response = Response.ok(list, MediaType.APPLICATION_JSON); } else if (mediaType.equals("xls")) { try { File file = File.createTempFile("export", ".xls"); response = Response.ok((Object) file, "application/vnd.ms-excel"); response.header("Content-Disposition", "attachment; filename=" + file); file.deleteOnExit(); } catch (IOException ex) { throw new NotFoundException(ex.getMessage()); } } else { throw new NotFoundException("Formato non supportato!"); } return response.build(); // http://192.168.1.30:8080/cr/test/xls // http://192.168.1.30:8080/cr/test/json } }
se gli passo json, vedo il json esattamente come negli altri service.
quindi diciamo che funziona.
ma:
- non è il massimo della felssibilità nel caso in cui dovessi aggiungere altri media type
- anche qui try/catch così non mi piacciono
avete qualche suggerimento al riguardo??

Rispondi quotando