0

I'm setting up a Rest Server along with client and i'm facing a problem for few hours already. When i call getPoints() method everything works fine, but when i call getPoint(Long id), deletePoint(Long id) and postPoint() i'm getting: Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 404 null.

Client Side:

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);
}

Server Side:

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);
}

}

What can cause the problem ? When i put on browser address: http://localhost:5000/points/1 i'm normally getting point with id 1.

3
  • I don't want to put this in an answer, but are you missing the trailing / Commented Jan 6, 2019 at 22:44
  • Thanks but that doesn't resolve the problem Commented Jan 6, 2019 at 22:47
  • You added the / on the server side instead of the client? See my answer please Commented Jan 6, 2019 at 22:48

2 Answers 2

1

The string you are concenating in your getPost() method will be: http://localhost:5000/points1 instead of http://localhost:5000/points/1.

Just add the /and you should be good to go

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that helped with GetMapping and DeleteMapping, PostMapping still not working but it might be problem with my code.
0

I'm not sure but can you try following:

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

Because i saw the post mapping in the server side: @PostMapping("/points/")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.