java - How to programmatically consume a file from a Rest API using Spring? -
i have next rest resource downloads file db. works fine browser, however, when seek java client below, 406 (not accepted error).
... @requestmapping(value="/download/{name}", method=requestmethod.get, produces = mediatype.application_octet_stream_value) public @responsebody httpentity<byte[]> downloadactivityjar(@pathvariable string name) throws ioexception { logger.info("downloading : " + name + " ... "); byte[] file = ioutils.tobytearray(artifactrepository.downloadjar(name)); httpheaders header = new httpheaders(); header.set("content-disposition", "attachment; filename="+ name + ".jar"); header.setcontentlength(file.length); homecoming new httpentity<byte[]>(file, header); } ...
the client deployed on same server different port (message gives right name) :
... resttemplate resttemplate = new resttemplate(); string url = "http://localhost:8080/activities/download/" + message.getactivity().getname(); file jar = resttemplate.getforobject(url, file.class); logger.info("file size: " + jar.length() + " name: " + jar.getname()); ...
what missing here?
the response code 406 not accepted. need specify 'accept' request header must match 'produces' field of requestmapping.
resttemplate resttemplate = new resttemplate(); resttemplate.getmessageconverters().add(new bytearrayhttpmessageconverter()); httpheaders headers = new httpheaders(); headers.setaccept(arrays.aslist(mediatype.application_octet_stream)); httpentity<string> entity = new httpentity<string>(headers); responseentity<byte[]> response = resttemplate.exchange(uri, httpmethod.get, entity, byte[].class, "1"); if(response.getstatuscode().equals(httpstatus.ok)) { fileoutputstream output = new fileoutputstream(new file("filename.jar")); ioutils.write(response.getbody(), output); }
java spring rest
No comments:
Post a Comment