How to get raw binary data from a POST request processed by Spring?
It's as easy as declaring an InputStream in your controller method's parameters:
@RequestMapping(method = RequestMethod.POST, value = "/data")public void acceptData(InputStream dataStream) throws Exception { processText(dataStream);}
You shouldn't need to disable HiddenHttpMethodFilter, if you do it's probably that your request is wrong in some way. See https://github.com/spring-projects/spring-boot/issues/5676.
I was able to resolve this using the following code:
@Beanpublic FilterRegistrationBean registration(HiddenHttpMethodFilter filter) { FilterRegistrationBean registration = new FilterRegistrationBean(filter); registration.setEnabled(false); return registration;}@RequestMapping(method = RequestMethod.POST, value = "/data")public void acceptData(HttpServletRequest requestEntity) throws Exception { byte[] processedText = IOUtils.toByteArray(requestEntity.getInputStream()); processText(processedText);}
Spring does pre-processing by default, which causes the HttpServletRequest
to be empty by the time it reaches the RequestMapping
. Adding the FilterRegistrationBean
Bean solves that issue.