-3

I cannot resolve this error; I added @CrossOrigin(origins="http://localhost:4200") but the error still persists.

RestAPIController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


(origins = "http://localhost:4200") 
@RestController
@RequestMapping("/api/customer")
public class RestAPIController {
    
    @Autowired
    CustomerServices customerServices;
    
    @PostMapping("/create")
    public ResponseEntity<Message> addNewCustomer(@RequestBody Customer customer) {
        try {
            Customer returnedCustomer = customerServices.saveCustomer(customer);
            
            return new ResponseEntity<Message>(new Message("Upload Successfully!", 
                                            Arrays.asList(returnedCustomer), ""), HttpStatus.OK);
        }catch(Exception e) {
            return new ResponseEntity<Message>(new Message("Fail to post a new Customer!", 
                                            null, e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);           
        }
    }
    .
    .
    .
    .
    
}

what do you advise me to do? Thank you

I have tried in every way, I have found and tried several examples on this site but the problem still appears.

a_l_e_x
  • 408
  • 1
  • 6
  • 20
  • 1
    Does this answer your question? [Spring CORS No 'Access-Control-Allow-Origin' header is present](https://stackoverflow.com/questions/35091524/spring-cors-no-access-control-allow-origin-header-is-present) – Avinash Apr 21 '21 at 12:32
  • 1
    The same question was already Answered. [Please take a look at it here](https://stackoverflow.com/questions/35091524/spring-cors-no-access-control-allow-origin-header-is-present#35092082) – Avinash Apr 21 '21 at 12:33
  • I have tried in all ways, I have tried several examples here but the problem still results. Otherwise I would not have asked this question – a_l_e_x Apr 21 '21 at 14:37

1 Answers1

1

This is an old question.

You should add this overide method in Config class which extend WebMvcConfigurer

        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("http://localhost:3000")
                .maxAge(3600);
   }
NTNT
  • 36
  • 4
  • I have tried in all ways, I have tried several examples here but the problem still results. Otherwise I would not have asked this question – a_l_e_x Apr 21 '21 at 14:36
  • also add http.cors() in SecurityConfig. If it still do not work. Just push link to source code here. maybe someone can help. – NTNT Apr 23 '21 at 14:33