簡體   English   中英

Spring MVC Rest Client獲得HttpClientErrorException:404 null

[英]Spring MVC Rest Client getting HttpClientErrorException: 404 null

我正在與客戶一起設置Rest Server,並且已經面臨幾個小時的問題。 當我調用getPoints()方法時,一切正常,但是當我調用getPoint(Long id),deletePoint(Long id)和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我通常會得到ID為1的點。

您要在getPost()方法中包含的字符串將是: http://localhost:5000/points1而不是http://localhost:5000/points/1

只需添加/就可以了

我不確定,但是您可以嘗試以下方法:

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

因為我在服務器端看到了帖子映射: @PostMapping("/points/")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM