Spring Boot FAQ: Top Questions
25. How do you configure Cross-Origin Resource Sharing (CORS) in Spring Boot?
Spring Boot supports CORS configuration at both global and controller level using annotations or configuration classes.
📘 Methods:
- Using
@CrossOrigin
annotation: Apply directly on controller or method. - Global config: Implement a WebMvcConfigurer bean.
📥 Example:
@CrossOrigin(origins = "http://localhost:3000")
@GetMapping("/data")
public List<Data> getData() { return dataList; }
🏆 Expected Output:
Allows requests from frontend on specified origin.
🛠️ Use Cases:
- Allowing frontend-backend interaction across domains.
- Enabling API usage from web clients in development.