ARTICLE AD BOX
I have a situation where I need to sometimes return a stream (ResponseEntity<StreamingResponseBody>), and other times some other (synchronous) value (ResponseEntity<String>, etc.).
When ResponseEntity<StreamingResponsebody> set directly as the return type of the @RequestMapping method, the stream is written correctly and received by the client. However, to support the other case I would like to use ResponseEntity<?> and allow effectively arbitrary response types.
When I do so, other return types work correctly except StreamingResponseBody, which now throws an exception:
org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.example.demo.dav.WebDavController$$Lambda$1110/0x000000080077a840] with preset Content-Type 'application/octet-stream'Is there anything I can do to resolve this?
Full example below:
@RestController public class WebDavController { @RequestMapping("/{filename}") public ResponseEntity<?> request(HttpServletRequest request, HttpServletResponse response, @PathVariable String filename) { val method = request.getMethod(); switch (method) { case "GET": return handleGet(request, response, filename); case "PROPFIND": return handlePropFind(request); default: return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).build(); } } //@GetMapping("/{filename}") private ResponseEntity<StreamingResponseBody> handleGet(HttpServletRequest request, HttpServletResponse response, @PathVariable String filename) { StreamingResponseBody responseBody = outputStream -> Files.copy(filename, outputStream); return ResponseEntity.ok() .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(responseBody); } private ResponseEntity<String> handlePropFind(HttpServletRequest request) { return ResponseEntity.ok() .body("this is a test"); } }