Я создал простой конечный пункт, чтобы начать работу с CORS в Spring MVC.Spring MVC CORS: Connection failed
Контроллер:
@Controller
@RequestMapping(value = "/api")
public class ApiController {
@Autowired
ItemService itemService;
@RequestMapping(value = "/items", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
HttpEntity<PagedResources<ItemResource>> items(Pageable pageable, PagedResourcesAssembler assembler) {
org.springframework.data.domain.Page<Item> items = itemService.findAll(pageable);
return new ResponseEntity<PagedResources<ItemResource>>(assembler.toResource(items), HttpStatus.OK);
}
}
Конфигурация:
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedOrigins("*")
.allowedMethods("*")
.allowedHeaders("*")
.allowCredentials(false)
.maxAge(3600);
}
Затем я проверил мой конечную точку с помощью Jquery AJAX:
$.ajax({
type: 'GET',
url: "http://test.dev:8080/api/items"
}).done(function(result) {
console.log(result);
}).fail(function(jqXHR){
console.log(jqXHR);
});
Тестирование с использованием жрать (PHP):
$client = new \GuzzleHttp\Client();
$response = $client->get('http://test.dev:8080/api/items');
JQuery AJAX и Gzzle работают отлично. Но когда я создал новое приложение Spring MVC, чтобы сделать еще один тест с моим API, я обнаружил ошибку:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://test.dev:8080/api/items":Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
Тест вызова с использованием RestTemplate:
RestTemplate restTemplate = new RestTemplate();
return restTemplate.getForObject("http://test.dev:8080/api/items", SampleResponse.class);
Где я должен начать, чтобы исправить эту проблему? Заранее спасибо!