简体   繁体   中英

Incomplete text from @PathVariable Java Spring

I'm trying to send an ID to my controller using:

<a href="/host/admin/sensor/downloadCSV/${sensor.id}" class="btn btn-primary"> Export CSV</a>

The content of ${sensor.id} variable is: testApp_provider.osom_component2.RT3

In my controller I'm getting it with:

@RequestMapping("/downloadCSV/{sensorId}")
public ModelAndView handleRequestInternal(HttpServletResponse response, @PathVariable final String sensorId) throws IOException {
    System.out.println("sensor:"+sensorId);
    return null;
}

But the output of the println is:

sensor:testApp_provider.osom_component2

I'm losing the last part: .RT3

Any Ideas?

You need to change it to,

@RequestMapping("/downloadCSV/{sensorId:.+}")
public ModelAndView handleRequestInternal(HttpServletResponse response, @PathVariable final String sensorId) throws IOException {
    System.out.println("sensor:"+sensorId);
    return null;
}

As everything behind the last dot is a file extension for Spring, so it truncates it by default.

The other, global solution would be to set useRegisteredSuffixPatternMatch property to false when registering the RequestMappingHandlerMapping .

More cleaner way however is to add trailing / slash at the end

@RequestMapping("/downloadCSV/{sensorId}/")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM