1

I have a button which sends the filename to API, but API gets only name of file (not included extension). How do I get extension?

JS

<button class="upl-btn" data-url="validacao/upload/planilha/55163343b0df070bbc66e1bb6e0c3f9b.xlsx" data-type="DELETE">
<span>Apagar</span>
</button> 

API

@RestController
@RequestMapping("/validacao/upload/planilha")
@Api(hidden = true, value = "/planilha", description = "API para envio de planilhas para validação")
public class UploadPlanilhaAPI {
    @RequestMapping(value = "/{name}",  method=RequestMethod.DELETE)
        public Response deleteFile(@PathVariable("name")String name){
            HttpSession session = activeUser.getSession();
            String path = (String) session.getAttribute("dir");
            uploadPlanilhaService.deletePlanilha(path, name);
            session.removeAttribute("json");
            session.removeAttribute("ignored");
            session.setAttribute("lastuploaddate", Util.now());
            return Response.ok().build();
        }
}

Result is name = 55163343b0df070bbc66e1bb6e0c3f9b, I wanna name = 55163343b0df070bbc66e1bb6e0c3f9b.xlsx

Daniela Morais
  • 2,125
  • 7
  • 28
  • 49

1 Answers1

2

You are encountering Spring's optional suffixes feature. By default, Spring lets you declare a /myPage request mapping. Now it lets you call /myPage.jsp, /myPage.json and Spring handles the translation of the response into the required type. It's quite annoying that they've switched it on by default; especially if the final part of your URL is a path variable: it ends up trimming the final . part off because it thinks you're trying to use this feature.

If you aren't interested in this feature, you can turn it off.

In your servlet xml file, add this path-matching setting:

<mvc:annotation-driven>
    <mvc:path-matching registered-suffixes-only="true"/>
</mvc:annotation-driven>

Alternative solutions here: Spring MVC @PathVariable with dot (.) is getting truncated

Community
  • 1
  • 1
David Lavender
  • 8,021
  • 3
  • 35
  • 55