https://kotlinlang.org logo
Title
s

Sandeep Kumar

10/22/2019, 11:18 PM
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.
@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
<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>
k

karelpeeters

10/22/2019, 11:30 PM
#spring
c

corneil

10/23/2019, 7:37 AM
Do you have processing instructions in your xml file?
It should work by just loading xml in browser with xslt accessible in same or relative folder
b

bjonnh

10/23/2019, 6:10 PM
Did you check what contenttype is given ? (use devtools in firefox for example)
Network in Developer Tools
(in the @requestmapping)
If I remember correctly, spring has a way to serve your files directly and then set types accordingly
the way you did that I think you have to access the response.outputstream and flush it. I'm not sure it works by just sending response
s

Sandeep Kumar

10/24/2019, 1:57 AM
Thanks for your responses. I have used more or less this code from Spring+Java for a while. I don’t set the mime type explicitly in that code and FileSystemResource seems to take care of it. So I don’t know what’s going on. But if there’s a better way to return static files, please let me know. The returned mime-type is always application/json. But let me actually re-do this experiment with the latest Spring+Java to make sure that adding Kotlin is what’s producing this effect. I also don’t understand why the controller is being called twice. Maybe it’s something in my environment.
b

bjonnh

10/24/2019, 2:57 AM
you would have to share a small project that fails to figure that last one out that.
c

corneil

10/24/2019, 6:40 AM
You can place static content in src/main/resources/static or configure a resource loader.