RESTful webservice using Spring mvc will answer data file as answer

How can I reply back with a text file containing json data as requested by the client.

Request URL:

http://localhost:8082/web/ws/datafileid/json/Sat May 16 12:05:07 IST 2015.txt/ 

Controller code processing the request:

 @RequestMapping(value=EmpRestURIConstants.DATAFILE_REQUEST,method=RequestMethod.GET) @ResponseBody public String datafileresponse(@PathVariable("filename") String filename, HttpServletResponse response) { return cinehomeRestService.checkfilevalid(filename); } 

The service class that processes the file check request exists:

 @Override public String checkfilevalid(String filename) { String datafilename=webServiceDao.getdatafilename(); JSONObject obj = new JSONObject(); if(datafilename.equals(filename)) { return "file"; } else { try { obj.put("status", "022"); } catch (JSONException e) { e.printStackTrace(); } return obj.toString(); } } 

Here I need to answer with datafile.txt that exists in the resources location. How to complete the task. Can anyone help?

I tried the method

 @RequestMapping(value=EmpRestURIConstants.DATAFILE_REQUEST,method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE) public @ResponseBody Response datafileresponse(@PathVariable("filename")String filename) throws IOException{ JSONObject readdata = new JSONObject(); String uploadPath = servletContext.getRealPath(""); String fullyqualifiedfilename=uploadPath+filename; System.out.println("+++++++++"+fullyqualifiedfilename); return Response.ok(uploadPath) .header("Content-Disposition", "attachment; filename=\"" + fullyqualifiedfilename + "\"" ) //optional .build(); } 

i CONTACT HOW TO ...

 { "statusType": "OK", "entity": /home/cine/WORKSPACES/study/.metadata/.plugins/org.eclipse.wst.server.β€Œβ€‹core/tmp0/wtpwebapps/ "entityType": "java.lang.String", "metadata": { "Content-Disposition": [ "attachment; filename="/home/cine/WORKSPACES/study/.metadata/.plugins/org.eclipse.wst.server.β€Œβ€‹core/tmp0/wtpwebapps/Cine Sat May 16 12:05:07 IST 2015.txt"" ] }, "status": 200 } 

What does this status mean. Can the client extract this file with this answer.

0
rest file spring-mvc web-services response
May 18 '15 at 6:23
source share

No one has answered this question yet.

See similar questions:

333
Download file from spring controllers
65
what's the right way to send a file from a REST web service to a client?

or similar:

2480
How do I send JSON data using Curl from a terminal / command line in Test Spring REST?
615
Posting a file and related data to a RESTful WebService, preferably as JSON
412
What is the difference between text / xml vs application / xml for webservice response
351
What is @ModelAttribute in Spring MVC?
249
RESTful authentication through Spring
237
Spring MVC @PathVariable with dot (.) Truncated
186
REST response code for invalid data
2
Content type "null" is not supported by RESTTemplate getForObject returning Spring



All Articles