Description of spring-boot-starter-websocket does not make it clear that it's Servlet-specific
Simple multipart/form-data file upload rejects with Content type 'application/octet-stream' not supported for a file part with application/octet-stream when websocket dependency is configured.
I tried the example described here https://vinsguru.medium.com/spring-webflux-file-upload-f6e3f3d3f5e1 with the latest spring 2.7.4 from spring boot starter. It worked. After I added the following dependency it breaks:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
Sample Upload Controller method:
@PostMapping(path = "file/single", consumes = {"multipart/form-data"})
public Mono<Void> upload(@RequestPart("user-name") String name, @RequestPart("fileToUpload") Mono<FilePart> filePartMono) {
System.out.println("user : " + name);
return filePartMono
.doOnNext(fp -> System.out.println("Received File : " + fp.filename()))
.flatMap(fp -> fp.transferTo(basePath.resolve(fp.filename())))
.then();
}
Sample Upload form:
<form class="mt-3" action="upload/file/single" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>User</label>
<input type="text" name="user-name"> <br /><br />
<label>Single</label>
<input type="file" name="fileToUpload" id="fileToUpload1">
</div>
<div class="form-group mt-3">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
There was an unexpected error (type=Unsupported Media Type, status=415).
Content type 'application/octet-stream' not supported
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:211)
at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:140)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
Thanks for getting in touch, but it feels like this is a question that would be better suited to StackOverflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
In this case I think you're conflating Spring MVC and Spring WebFlux. Adding the websocket dependency is only required for MVC and actually brings in MVC, turning your entire application as a Spring MVC app. You don't need the websocket starter to have websocket with WebFlux.
Thanks for explanation. I have to admin that I used https://start.spring.io/ to create the project config. The starter page allows to select Spring Reactive Web together with WebSocket. Any chance that the start page gray out give a hint that it should not be used together?
It's not an invalid combination in itself. One could work on a Servlet based application using websocket and still need WebFlux for the WebClient.
If anything the starter name should be changed in Spring Boot but it's too late for that. You could also suggest a better dependency description on start.spring.io for the websocket starter as it doesn't mention MVC at all for now. See https://github.com/spring-io/start.spring.io/issues
I agree that it's probably too late to rename the starter. We could still improve its description in the reference documentation though.