2

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.

2 Answers 2

1

Unfortunately, you cannot have two response bodies at the same time. You can have it either set to text/csv or to text/json and you can return a single response body at a time.

However, you can convert your CSV into bytes and Base64.enocode it and put inside of the object. You can also store your CSV in a string and return it with a custom line separator.

Sign up to request clarification or add additional context in comments.

2 Comments

I do not have a CSV already available, I need to create it in the code so I believe Base64.encode will not be the appropriate way to do it. Any idea how the string can be interpreted as csv file by the caller method which is java fetch call?
Okay, if you can't to make your file downloadable and return it as a json. However, you can return json object with csv inside one of the properties and on the frontend parse that property, create csv and send it to a client. This is one of the examples
0

Serialize your class MyReturnObject use a jackson library or gson in you project.

@RequestMapping(value = "/api/fetchInfo", method = RequestMethod.GET)
public @ResponseBody MyReturnObject fetchInfoRestControllerMethod (String param1) {
    return MyReturnObject.builder()
                                .countOfRows(x)
                                .csvDataFile(csvFile)
                                .errorMessage(y).build();
}

1 Comment

Sorry, I am unable to understand how can I retain csv file after the serialization of object and how can file be serialized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.