0

Using spring-web, I am mapping a method to receive a request containing dots "." on the path:

@RequestMapping(value = "download/{id:.+}", method = RequestMethod.GET, produces = "application/xls")
public String download(@PathVariable(value = "id") String id) { ... }

For example, /download/file.xls should be a valid address. But when I try to access that address, Spring returns Could not find acceptable representation as if it was trying to find a resource named file.xls.

Spring shouldn't execute download method rather than try to find a resource named as the path variable?

Obs.: my application is a spring-boot application.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Marcos Tanaka
  • 3,112
  • 3
  • 25
  • 41

2 Answers2

1

Your @RequestMapping says it produces "application/xls", but your return type is a String and you haven't annotated the return type with @ResponseBody.

If you want to return an Excel spreadsheet, you need to produce that spreadsheet on the server and return it as a byte[] from your request mapping. I'm not sure how or why you'd return a String, unless you're controller is a simple @Controller and you're returning the view name.

Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
0

Have you tried configuring your RequestMappingHandlerMapping

handler.setUseSuffixPatternMatch( false )

(I was configuring my RequestMappingHandlerMapping anyway, so for me I just needed to add that line - chances are you may be letting Spring Boot autoconfig that class).

See https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerMapping.html#setUseRegisteredSuffixPatternMatch-boolean-

Possibly you may need to turn off content negotiation as well - I can't remember exactly what Spring Boot default content negotiation is, but it might be affecting your case.

@Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.favorPathExtension(false)
}

Worth noting that if you are working on a wider/existing application then both these configurations have possible implications more widely, so if that is the case then tread carefully!

rhinds
  • 9,976
  • 13
  • 68
  • 111