Abdus Samad
Abdus Samad

Reputation: 353

HTTP Status 405 - Request method 'POST' not supported

I am using spring, spring security, hibernate. Got a jsp page where i am trying to upload a file, and backend a i have a controller to capture and store the file uploaded. I am using tomcat. I am using spring security for login authentication. Getting the following error when i upload the file HTTP Status 405 - Request method 'POST' not supported Any ideas?

Upvotes: 2

Views: 8997

Answers (2)

Sergey Tselovalnikov
Sergey Tselovalnikov

Reputation: 6036

Just define bean "multipartResolver" in your Spring Context

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- one of the properties available; the maximum file size in bytes -->
        <property name="maxUploadSize" value="2097152"/>
    </bean>

And use

@ResponseBody
@RequestMapping(value = "/{tenantId}/getEntityInfo", method = RequestMethod.POST)
public ResponseEntity<String> getEntityInfo(
        @RequestParam(value = "xml", required = false) MultipartFile xml) {
}

Upvotes: 0

aweigold
aweigold

Reputation: 6879

You will need to ensure your request handler is able to accept a POST. You can also configure Spring to use a MultipartResolver to aid you in getting your request parts.

Configuration of MultiPartResolver

@Bean(name = "mulitpartResolver")
public MultipartResolver multipartResolver() {
    if (multipartResolver == null) {
        multipartResolver = new CommonsMultipartResolver();
    }
    return multipartResolver;
}

Here is the request mapping:

@RequestMapping(method = RequestMethod.POST, value = "/some/post/url")
public void postFile(MultipartHttpServletRequest request) {
    MultipartFile multipartFile = request.getFileMap().get("keyForFileInFormPost");
    ...
}

Note, that sometimes this will not work with Spring Security. You can look at my blog post here on using multipartrequestresolvers with spring security for help:

http://www.adamweigold.com/2012/01/using-multpartrequestresolvers-with.html

Upvotes: 4

Related Questions