0

Access to XMLHttpRequest at 'http://localhost:8084/Restaurent_Management/rest/admin/search/1001' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I am using Angular in front end and java in back end.

This is my Controller

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.cg.rm.entities.Admin;
import com.cg.rm.service.AdminService;

import com.cg.rm.service.CustomerService;
import com.cg.rm.entities.Customer;


@RestController
@CrossOrigin(origins = "*") //This is not working
public class RestaurentController {

    @Autowired
    CustomerService customerService;

    @Autowired
    AdminService adminService; 

    @RequestMapping(value ="/admin/search/{id}",headers="Accept=application/json",method = RequestMethod.GET)
    public Admin searchAdmin(@PathVariable("id") int id) {
        System.out.println("In search");
        return adminService.searchAdmin(id);
    }

    @RequestMapping(value = "/customer",method = RequestMethod.GET,headers="Accept=application/json")
    public List<Customer> getAllCustomer(Model model) {
        return customerService.getAllCustomer();
    }

    @RequestMapping(value = "/customer/delete/{id}",
            headers="Accept=application/json",method = RequestMethod.DELETE)
    public List<Customer> deleteEmployee(@PathVariable("id") int id) {
        System.out.println(id);
        customerService.deleteCustomer(id);
        return customerService.getAllCustomer();
    }
    @RequestMapping(value ="/customer/create/",consumes = MediaType.APPLICATION_JSON_VALUE,headers="Accept=application/json",method = RequestMethod.POST)
    public List<Customer> createCustomer(@RequestBody Customer cust) {

        System.out.println("hiiii");
        System.out.println(cust);
        customerService.addCustomer(cust);
        return customerService.getAllCustomer();
    }
    @RequestMapping(value ="/customer/search/{id}",headers="Accept=application/json",method = RequestMethod.GET)
    public Customer searchCustomer(@PathVariable("id") int id) {
        System.out.println("In search");
        return customerService.searchCustomer(id);
    }
    @RequestMapping(value ="/customer/update/",consumes = MediaType.APPLICATION_JSON_VALUE,headers="Accept=application/json",method = RequestMethod.PUT)
    public List<Customer> updateCustomer(@RequestBody Customer cust) {

        System.out.println("Update");
        System.out.println(cust);
        customerService.updateCustomer(cust);
        return customerService.getAllCustomer();
    }
}

This is dispatcher servlet

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


<!-- To enable Spring Components -->
    <context:component-scan base-package="com.cg.rm" />


    <!-- Mapping for Spring ViewResolver  -->
     <mvc:annotation-driven/>

</beans>
Ritu Raj
  • 3
  • 2
  • Your path starts with `rest`. Where is the path set? It should be on the Controller class as `@RequestMapping(value = "/rest")` – Nikhil May 10 '20 at 16:07

1 Answers1

0

If you are using Spring Security, remember to do enable CORS on Spring Security level

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()...
    }
}

Source: https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

However

If you're using Tomcat instead of SpringBoot's embedded Tomcat, then you should probably set CORS on Tomcat level Set CORS header in Tomcat

Tan Kim Loong
  • 980
  • 7
  • 14