Java Spring MVC Rest客户端获取HttpClientErrorException:404 null
我正在安装一个Rest服务器和一个客户端,我已经面临了几个小时的问题。当我调用getPoints()方法时,一切正常,但当我调用getPoint(Long id)、deletePoint(Long id)和postPoint()时,我在线程“main”org.springframework.web.client.HttpClientErrorException:404null中得到了:Exception 客户端:Java Spring MVC Rest客户端获取HttpClientErrorException:404 null,java,spring,model-view-controller,http-status-code-404,Java,Spring,Model View Controller,Http Status Code 404,我正在安装一个Rest服务器和一个客户端,我已经面临了几个小时的问题。当我调用getPoints()方法时,一切正常,但当我调用getPoint(Long id)、deletePoint(Long id)和postPoint()时,我在线程“main”org.springframework.web.client.HttpClientErrorException:404null中得到了:Exception 客户端: public List<Point> getPoints() {
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 List getPoints()
{
RestTemplate RestTemplate=新RestTemplate();
ResponseEntity pointResponse=restTemplate.exchange(“http://localhost:5000/points,HttpMethod.GET,null,
新的ParameteredTypeReference()
{});
List points=pointResponse.getBody();
返回(点数);
}
公共点获取点(长id)
{
RestTemplate RestTemplate=新RestTemplate();
Point=restTemplate.getForObject(“http://localhost:5000/points“+id,Point.class);
返回点;
}
公共空后点()
{
RestTemplate RestTemplate=新RestTemplate();
restTemplate.postForObject(“http://localhost:5000/points,this,this.getClass());
}
公共无效删除点(长id)
{
RestTemplate RestTemplate=新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);
}
公共类PointsController{
私有最终点存储库;
PointsController(PointsRepository存储库){
this.repository=repository;
}
@GetMapping(“/points/”)
列出所有(){
返回repository.findAll();
}
@后映射(“/points/”)
Points newPoints(@RequestBody Points newPoints){
返回repository.save(newPoints);
}
@GetMapping(value=“/points/{id}/”,products=“application/json;charset=UTF-8”)
资源一(@PathVariable Long id){
Points point=repository.findById(id).orelsethorn(()->new PointsNotFoundException(id));
返回新资源(点,
链接到(methodOn(PointsController.class).one(id)).withSelfRel(),
链接到(methodOn(PointsController.class).all()).withRel(“points”);
}
@删除映射(“/points/{id}/”)
void deletePoints(@PathVariable Long id){
repository.deleteById(id);
}
}
什么会导致问题?当我打开浏览器地址时:我通常得到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());
}
因为我在服务器端看到了post映射:@PostMapping(“/points/”)
我不想把这一点放在回答中,但是您是否缺少尾部的/
谢谢,但这并不能解决问题您在服务器端而不是客户端添加了/?请看我的答案,这有助于GetMapping和DeleteMapping,PostMapping仍然不起作用,但我的代码可能有问题。