Here is my webConfig
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}
I have an endpoint /healththat returns some health data and works fine.
Here is the junit test
@Test
public void corsTest() {
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ORIGIN, "http://localhost:9000");
HttpEntity<?> requestEntity = new HttpEntity(headers);
ResponseEntity<Status> entity = this.restTemplate.exchange("/health", HttpMethod.GET, requestEntity, Status.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
assertEquals("http://localhost:9000", entity.getHeaders().getAccessControlAllowOrigin());
Status health = entity.getBody();
assertThat(health.getAppName()).isEqualTo(appName);
}
The test fails on line 6. If you comment out this line, the test passes. Through the debugger, I found that entity.getHeaders().getAccessControlAllowOrigin()it is null. I used this sample application as a reference and it works great. but for my application it does not work.
when i use javascript to call / health api i see cors working. If I comment on the webConfig class, I get an error in javascript. Debugging more on javascript shows that the cors response header is not set, but the request worked successfully when webconfig is enabled.
cors. , . , , .