Monday 2 March 2020

Spring REST: Multipart request for PUT Mapping

By default, Spring REST allows only POST for multipart request. To allow multipart payload for PUT request, you should create 'MultipartResolver' bean like below.

@Bean
public MultipartResolver multipartResolver() {
    return new StandardServletMultipartResolver() {
        @Override
        public boolean isMultipart(HttpServletRequest request) {
           String method = request.getMethod().toLowerCase();
           //By default, only POST is allowed. Since this is an 'update' we should accept PUT.
           if (!Arrays.asList("put", "post").contains(method)) {
              return false;
           }
           String contentType = request.getContentType();
           return (contentType != null &&contentType.toLowerCase().startsWith("multipart/"));
        }
      };
}


Previous                                                    Next                                                    Home

No comments:

Post a Comment