I’m hoping to get some pointers that will help me ...
# spring
s
I’m hoping to get some pointers that will help me work around this issue. I’m having trouble getting the following simple-minded controller to work correctly with Spring Framework 5.2, Springboot 2.2 and Kotlin 1.3.50. It’s a simple FileServerController that serves files below ~ but I seem to get double invocations of the controller and the mime-type of the returned file is always application/json even when I set it explicitly (as below). If I don’t return xml files with the correct mime-type, then I can’t get the browser to perform XSL transformations on it. I have a feeling (but not positive) that the non-Kotlin version of it would work. But I want to get started on Kotlin.
Copy code
@Controller
class FileServerController {
    @RequestMapping(value = ["/**"])
    fun getFile(request: HttpServletRequest, response: HttpServletResponse): FileSystemResource {
        var fileName = request.servletPath
        val HOME = System.getenv("HOME")

        println("Requested ${fileName}")

        fileName = "${HOME}${fileName}"
        val file = File(fileName)

        if (file.canRead()) {
            println("Serving ${fileName}")
            if(fileName.endsWith(".xml")) {
                response.contentType = "text/xml"
            } else if(fileName.endsWith(".xslt")) {
                response.contentType = "application/xslt+xml"
            }
            return FileSystemResource(file)
        } else {
            println("Couldn't find ${fileName}")
            throw NotFoundException(fileName)
        }
    }
}
The dependencies in my pom.xml are
Copy code
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-reflect</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-stdlib-jdk8</artifactId>
    </dependency>
Thread in Slack Conversation