java - Spring MVC Rest Client 获取 HttpClientErrorException: 404 null
问题描述
我正在与客户端一起设置休息服务器,我已经面临几个小时的问题。当我调用 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
.
只需添加 就/
可以了
推荐阅读
- javascript - 如何在带有 gatsby 的 graphql 查询中使用正则表达式
- django - django 在表单字段中使用多个小部件
- python - 如何在 PyQt5 中创建两个或多个颜色自定义 QPushButton?
- python - 改组 numpy 数组,保持各自的值
- c++ - C++ 为什么引用一个对象?
- amazon-web-services - AWS 应用程序扩展 - 分步扩展策略
- python - 具有级联 AmbiguousForeignKeysError 的另一个表中的多个相同外键
- c# - 在 asp.net 中使用异步加载页面
- php - 如何更改 SQL SELECT 中的日期格式?
- reactjs - API 结果不一致