I am trying to create a rest controller which returns an object which contains a String, Long and a csv file. I am unable to do it because as soon I write the file creation logic in java, rather than returning it, it creates a file on my host.
I have already tried to write the entire object to the httpservletresponse using writer and output stream, but it did not give the expected result. I returned the object from controller, but instead of returning the object, it created a file on my host and did not return other values at all.
public class MyReturnObject {
private Long countOfRows;
private File csvDataFile;
private String errorMessage;
}
@RequestMapping(value = "/api/fetchInfo", method = RequestMethod.GET)
public MyReturnObject fetchInfoRestControllerMethod (String param1) {
//some logic/ service calls
return createMyReturnObject(values);
}
public MyReturnObject createMyReturnObject(List<CustomObject> values) {
File csvFile = new File("myinfo.csv");
file.createNewFile();
FileWriter writer = new FileWriter(csvFile);
writer.write("test");
//some lines
MyReturnObject returnObject = MyReturnObject.builder()
.countOfRows(x)
.csvDataFile(csvFile)
.errorMessage(y).build();
return returnObject;
}
This is just some dummy code I wrote. The object that I am building in createMyReturnObject
is not returning the expected result. I am thinking if it is possible to return something like object MyReturnObject
from a rest controller. Please help me with ideas to get expected results.