-1

I've a SpringBoot Controller as follows

@CrossOrigin(origins="*")
public class FormController {
      @PostMapping(path="/basicForm")
 public String postResponseController(
              @RequestBody FormDomain loginForm) {
        System.err.println("basic form method called");
         return "file transfer completed successfully";
     }
}

I've annotated the controller with @CrossOrigin(origins="*") to enable CORS. Here is my FormDomain Class

public class FormDomain {
     private MultipartFile fileInput;

    public MultipartFile getFileInput() {
        return fileInput;
    }

    public void setFileInput(MultipartFile fileInput) {
        this.fileInput = fileInput;
    }
    }

And the angular script calling the controller method

 onClickSubmit(data) {
        this.http.post("http://localhost:8080/basicForm", data).subscribe( (ob)=>(console.log(ob)));
      }

Eventhough I've annotated the controller to enable CORS I get following error in browser console

Access to XMLHttpRequest at 'http://localhost:8080/basicForm' 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.

How to solve this issue?

Arjun
  • 1,116
  • 3
  • 24
  • 44

1 Answers1

2

You have to add @RestController in FormController

@Controller a specialization of the @Component class and allows implementation classes to be autodetected through the classpath scanning. while @RestController is a specialization of the @Controller which eliminates the need for @ResponseBody. You can refer @Controller, RestController

Romil Patel
  • 12,879
  • 7
  • 47
  • 76