Spring MVC : Return CSS

css, java, spring, spring-mvc

Solution

I suspect the problem is due to your usage of `HttpServletResponse.flushBuffer()`.

As the API of `HttpServletRequest` states:

Forces any content in the buffer to be written to the client. A call to this method automatically commits the response, meaning the status code and headers will be written.

My assumption would be that Spring attempts to set the `Content-Type` header on the `HttpServletResponse` after the method on your controller has returned. However, because you have committed the response with your call to `HttpServletResponse.flushBuffer()`, it cannot do this.

I would try either:

- Injecting the HttpServletResponse into your controller and setting the header yourself in code before calling `HttpServletResponse.flushBuffer()`

- Removing your usage of `HttpServletRequest.flushBuffer()`

Problem

My goal is to merge/minify all css files and return the result as String. Here's my Spring test method : ``` @RequestMapping(value = "/stylesheet.css", method = RequestMethod.GET, produces = "text/css") @ResponseBody public void css(HttpServletResponse response) { File path = new File(servletContext.getRealPath("/WEB-INF/includes/css/")); File[] files = path.listFiles(...); for (File file : files) { InputStream is = new FileInputStream(file); IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); is.close(); } } ``` This is working with Chrome, Firefox and Safari but not with IE and Opera. After some checks in the inspectors, the URL `https://host/project/stylesheet.css` is loading in each browsers. I can see the content but it does not seem to be recognized as `text/css`. Also, even with `produces = "text/css"`, I can not see the `content-type` http header in all browsers. Error log in IE : ``` CSS ignored because of mime type incompatibility ``` Does anyone know how to correctly do this? Working code : ``` @RequestMapping(value = "/stylesheet.css", method = RequestMethod.GET) public ResponseEntity<Void> css(HttpServletResponse response) { response.setContentType("text/css"); File path = new File(servletContext.getRealPath("/WEB-INF/includes/css/")); File[] files = path.listFiles(...); for (File file : files) { InputStream is = new FileInputStream(file); IOUtils.copy(is, response.getOutputStream()); IOUtils.closeQuietly(is); } response.flushBuffer(); return new ResponseEntity<Void>(HttpStatus.OK); } ```

Original source

Related problems