-1

Calling a java api build with STS and spring MVC from angular in visual studio code, we are getting this error:

Access to XMLHttpRequest at 'http://localhost:8081/HelloWorld/list' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"http://localhost:8081/HelloWorld/list","headers":{"Accept":"application/json, text/plain, /"}},"statusText":"","xhrStatus":"error"}

1

2 Answers 2

2

Let's consider your angular application is running at port number 9000. Now, if you want to call your Spring API(that is running at a different port) then you need to add below annotation at controller.

@CrossOrigin(origins = "http://localhost:9000")

Please let me if works for you or otherwise.

Sign up to request clarification or add additional context in comments.

Comments

0

Spring3CorsFilter.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Spring3CorsFilter {}

Spring3CorsFilterHandlerInterceptor.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class Spring3CorsFilterHandlerInterceptor extends HandlerInterceptorAdapter {


     @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws
                Exception {

            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                // Test if the controller-method is annotated with @Spring3CORSFilter
                Spring3CorsFilter filter = handlerMethod.getMethod().getAnnotation(Spring3CorsFilter.class);
                if (filter != null ) {
                    // ... do the filtering
                    response.setHeader("Access-Control-Allow-Origin", "http://localhost:4200");
                    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
                    response.setHeader("Access-Control-Max-Age", "3600");
                    response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
                }
            }
            return true;
        }

}

Controller.java

@Spring3CorsFilter
    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.GET,headers="Accept=application/json")
    public List <SuiviCommande> getSearch() int L, @RequestBody SuiviCommandeDto A) {

         ....

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.