Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request method 'OPTIONS' not supported - Spring Boot Application

I am using Spring Boot in the server side and Angularjs in client side, i configured Spring Boot application CORS using a Filter and it worked for GET, POST methods, but, when i tried to send PUT request using $http module, i got this in my browser console :

Notice: url = http://localhost:8080/localbusinessusers/[object%20Object]

OPTIONS url (anonymous function) @ angular.js:11442sendReq @ angular.js:11235serverRequest @ angular.js:10945processQueue @ angular.js:15552(anonymous function) @ angular.js:15568Scope.$eval @ angular.js:16820Scope.$digest @ angular.js:16636Scope.$apply @ angular.js:16928done @ angular.js:11266completeRequest @ angular.js:11464requestLoaded @ angular.js:11405 app.html:1 XMLHttpRequest cannot load url. Response for preflight has invalid HTTP status code 405 angular.js:11442 XHR failed loading: PUT "url".

And i got this on Spring Boot console:

2016-03-08 23:19:51.212 WARN 27044 --- [ XNIO-2 task-2] o.s.web.servlet.PageNotFound : Request method 'OPTIONS' not supported

My CORS Filter:

package com.datcom.fouras;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

@Component
public class CorsFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "http://localhost");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header");
        chain.doFilter(req, res);

    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

}

My REST method in my RestController:

@RequestMapping(value = "/localbusinessusers", method = RequestMethod.PUT)
    public LocalBusinessUser update(LocalBusinessUser localBusinessUser) {
        log.debug("REST request to update a local business user : {}", localBusinessUser);
        if (localBusinessUser.getId() == null) {
            return save(localBusinessUser);
        } else {
            LocalBusinessUser localBusinessUser2 = localBusinessUserRepository.findOne(localBusinessUser.getId());
            localBusinessUser2 = localBusinessUser;
            return localBusinessUserRepository.saveAndFlush(localBusinessUser2);
        }
    }

And finally, my $http request:

$http.put('http://localhost:8080/localbusinessusers/' + localBusinessUser)
                .success(function (data, status, headers) {
                    console.log('data: ' + data);
                })
                .error(function (data, status, header, config) {
                    console.log('data: ' + data);
                    console.log('status: ' + status);
                    console.log('header: ' + header);
                    console.log('config: ' + config);
                });

Notice: PUT request is working using Advanced REST Client

like image 377
jemlifathi Avatar asked Mar 08 '16 22:03

jemlifathi


1 Answers

If Options requests are being prohibited, you can configure the DispatcherServlet to dispatch all options requests by adding this to your application.properties:

spring.mvc.dispatch-options-request=true

For more information about options requests and Spring MVC using Spring Boot, there is a nice post for Handling Options Request using Spring Boot

You can also try the examples in this post for handling CORS and supporting Options requests by creating a simple controller to return NO_CONTENT for all Options requests: http://dontpanic.42.nl/2015/04/cors-with-spring-mvc.html

like image 130
pczeus Avatar answered Oct 26 '22 23:10

pczeus