gpt4 book ai didi

java - Spring MVC Rest 客户端获取 HttpClientErrorException : 404 null

转载 作者:行者123 更新时间:2023-12-02 10:24:39 24 4
gpt4 key购买 nike

我正在与客户端一起设置休息服务器,但几个小时以来我已经遇到了问题。当我调用 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/点/1

只需添加 / 就可以开始了

关于java - Spring MVC Rest 客户端获取 HttpClientErrorException : 404 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54066540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com