Spring MVC Rest Client получает HttpClientErrorException: 404 null - PullRequest
0 голосов
/ 07 января 2019

Я устанавливаю Сервер Отдыха вместе с клиентом и уже несколько часов сталкиваюсь с проблемой. Когда я вызываю метод getPoints (), все работает нормально, но когда я вызываю getPoint (длинный идентификатор), deletePoint (длинный идентификатор) и postPoint (), я получаю: исключение в потоке "main" org.springframework.web.client.HttpClientErrorException : 404 ноль.

Клиентская сторона:

public List<Point> getPoints()
{
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<List<Point>> pointResponse = restTemplate.exchange("http://localhost:5000/points", HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Point>>()
            {});
    List<Point> points = pointResponse.getBody();
    return (points);
}

public Point getPoint(long id)
{
    RestTemplate restTemplate = new RestTemplate();
    Point point = restTemplate.getForObject("http://localhost:5000/points" + id, Point.class);
    return point;
}

public void postPoint()
{
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForObject("http://localhost:5000/points", this, this.getClass());
}

public void deletePoint(Long id)
{
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.delete("http://localhost:5000/points" + id);
}

Сторона сервера:

public class PointsController {

private final PointsRepository repository;

PointsController(PointsRepository repository){
    this.repository=repository;
}

@GetMapping("/points/")
List<Points> all() {
    return repository.findAll();
}

@PostMapping("/points/")
Points newPoints(@RequestBody Points newPoints){
    return repository.save(newPoints);
}

@GetMapping(value = "/points/{id}/", produces = "application/json; charset=UTF-8")
Resource<Points> one(@PathVariable Long id) {
    Points point = repository.findById(id).orElseThrow(() -> new PointsNotFoundException(id));

    return new Resource<>(point,
            linkTo(methodOn(PointsController.class).one(id)).withSelfRel(),
            linkTo(methodOn(PointsController.class).all()).withRel("points"));
}

@DeleteMapping("/points/{id}/")
void deletePoints(@PathVariable Long id) {
    repository.deleteById(id);
}

}

Что может вызвать проблему? Когда я добавляю адрес браузера: http://localhost:5000/points/1 я обычно получаю очки с идентификатором 1.

Ответы [ 2 ]

0 голосов
/ 07 января 2019

Я не уверен, но вы можете попробовать следующее:

public void postPoint()
{
  RestTemplate restTemplate = new RestTemplate();
  restTemplate.postForObject("http://localhost:5000/points/", this, this.getClass());
}

Потому что я видел пост-отображение на стороне сервера: @PostMapping("/points/")

0 голосов
/ 07 января 2019

Строка, которую вы концентрируете в своем методе getPost(), будет: http://localhost:5000/points1 вместо http://localhost:5000/points/1.

Просто добавьте / и вам будет хорошо идти

...