Anyone familiar with Spring and REST?
This should be a fairly simple exercise, but I'm just missing something. The idea is to use a ready made REST interface in
this address in order to offer a similar interface to handle information on games and their ratings.
The relevant classes are:
The controller.
Code:
@RestController
@RequestMapping("games")
public class GameController {
@Autowired
private GameRestClient gameRestClient;
// posts a new game (doesn't work)
@RequestMapping(method=RequestMethod.POST)
public Game postGame(@RequestBody Game game) {
return gameRestClient.create(game);
}
// gets all the games (works)
@RequestMapping(method=RequestMethod.GET)
public Collection<Game> listGames() {
return gameRestClient.findAll();
}
// gets a game by name (doesn't work)
@RequestMapping(value="/{name}", method=RequestMethod.GET)
public Game showGameByName(@PathVariable("name") String name) {
return gameRestClient.findByName(name);
}
// deletes a game (untested)
@RequestMapping(value="/{name}", method=RequestMethod.DELETE)
public Game deleteGame(@PathVariable("name") String name) {
Game game = gameRestClient.findByName(name);
gameRestClient.deleteByName(name);
return game;
}
// I'm not sure quite if this is the correct way to set the URI but it seems to work at least
@PostConstruct
public void giveUri() {
gameRestClient.setUri("http://wepa-scoreservice-heroku.herokuapp.com/games");
}
}
A service using the REST interface of the above mentioned address (using the class RestTemplate for it):
Code:
@Service
public class GameRestClient implements GameService {
private RestTemplate restTemplate;
private String uri;
public GameRestClient() {
this.restTemplate = new RestTemplate();
}
@Override
public void setUri(String uri) {
this.uri = uri;
}
@Override
public Game create(Game game) {
return restTemplate.postForObject(uri, game, Game.class);
}
@Override
public Game findByName(String name) {
return restTemplate.getForObject(uri, Game.class, name);
}
@Override
public void deleteByName(String name) {
restTemplate.delete(uri, name); // not quite sure if this is right or if I should find the corresponding game first, but that's a smaller problem)
}
// this works, the others don't (apart from the setUri method)
@Override
public Collection<Game> findAll() {
return restTemplate.getForObject(uri, List.class);
}
}
The GET method listGames() in the controller (and thus the findAll() method in the service) seemingly works just fine when I try it on localhost:8080/games. But if I pick any random game and try to see its info (on localhost:8080/games/game'sNameHere), I get a whitelabel error page:
The tests in the exercise also get error immediately when it tests the POST type method ( postGame(@RequestBody Game game) ). The error is as follows:
I really don't have any clue why those errors come. Or yeah, I think I get that the first error comes as it can't parse the JSON, but I really don't understand why it can't be parsed.
Anyone who could help please?
If needed, I can post the stacktrace and the code of the REST interface in the given address.
Sorry if anything's unclear. I'm not sure if I fully understand the assignment myself and it feels like I'm still slightly clueless about web stuff in general.