Bernie
Bernie

Reputation: 1651

Spring RequestMapping with timestamp in URL

I would like to have a dynamic URL in Spring for downloading CSVs, e.g. Bernhard_content_24Feb2017.csv. More formally this would be

{id}_content_{timestamp}.csv

Every request like Me_content_1Jan2017.csv or You_content_31Dec2017.csv should go to the method in the code below (getCsv(...)). Note, that I want to keep the suffix .csv . The user should download a file with a name like Me_content_1Jan2017.csv.

So I tried this in Spring:

@RequestMapping("{id}_content_{timestamp}.csv")
public void getCsv(@PathVariable String id, @PathVariable String timestamp, HttpServletResponse response) {
    response.getWriter().print(createCsv(id));
}

Unfortunately this doesn't work.

What would be the correct syntax for this? Given, that it's possible.

Thanks,

Bernhard

UPDATE:

I went for this solution (renaming the URL a little bit, now like Me_1Jan2017_content.csv):

@RequestMapping("{idAndTimestamp}_content.csv")
public void getCsv(@PathVariable String idAndTimestamp, HttpServletResponse response) {
    String id = idAndTimestamp.split("_")[0];
    response.getWriter().print(createCsv(id));
}

Upvotes: 3

Views: 1395

Answers (4)

Dimitar Pavlov
Dimitar Pavlov

Reputation: 300

I can't find a way to use complex definition for @PathVariable. Try getting the full file name in one variable and splitting the parts in your method. Something like this.

@RequestMapping("{fileName}")
    public void getCsv(HttpServletResponse response, @PathVariable String  fileName) {
    String[] parts = fileName.split(".")[0].split("_");
    String id = parts[0];
    String timestamp = parts[2];
}

Upvotes: 1

javasenior
javasenior

Reputation: 1815

Change your id and timestamp parameters like this.

@RequestMapping(value = "downloadCvs/{id}/{timestamp}", method = RequestMethod.POST)
public void getCsv(HttpServletResponse response, @PathVariable String id, @PathVariable String timestamp) {

    response.setContentType("text/csv"); 

    String reportName = "CSV_Report_Name_What_you_Want.csv"; 

    response.setHeader("Content-disposition", "attachment;filename="+reportName); 

    response.getOutputStream().print(createCsv(id)); 

    response.getOutputStream().flush();
}

Upvotes: 1

SachinSarawgi
SachinSarawgi

Reputation: 2692

Try something like this

@RequestMapping("{filename:.+}") //putting ".+" will keep ".csv" extension otherwise you will lose it.
public void getCsv(@PathVariable(fileName) String  fileName) {
    String[] parts = fileName.split("_");
    String id = parts[0];
    String timestamp = parts[2].split(".")[0];
}

If you want to make it more confined use:

@RequestMapping("{filename:[A-Za-z0-9]+(_content_)[A-Za-z0-9]+(.csv)}")

Hope it helps.

Upvotes: 1

Niku Hysa
Niku Hysa

Reputation: 76

you should try something like this

@RequestMapping(value="/{path}/**", method = RequestMethod.GET)
public void getCsv(@PathVariable("path") String path, HttpServletRequest request){
    //your code here...

}

Upvotes: 0

Related Questions