Hi ,

While developing REST Services , at a point @CrossOrigin and @RequestMapping is used at class level. But it not resolving Cross origin errors.

When @RequestMapping is removed at class level and maintain at method level. It working quite well. I was unable is understand what is problem in that. I read documentation and followed the same.

@RestController
@CrossOrigin
@RequestMapping("/students")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/")
    public ResponseEntity<List<Student>> getStudents() {
        return new ResponseEntity<List<Student>>(studentService.get(), HttpStatus.OK);
    }
}

Its not working as expected.

But below is working as expected

@RestController
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/students")
    public ResponseEntity<List<Student>> getStudents() {
        return new ResponseEntity<List<Student>>(studentService.get(), HttpStatus.OK);
    }
}

Comment From: bclozel

The subtle difference between your code snippets is the extra "/" in one case. Maybe changing your mapping definition to @GetMapping("") in the first snippet will make it work as you'd like?

For further questions, please use StackOverflow. If you believe this is a bug, please create an issue on the Spring Framework project with a sample application we can run.

Thanks!