I'm making a webapp using Giant Bomb API.
I'm stuck because I need to get a list of search results from GET request (using Unirest) and then convieniently convert it to a List of Objects called suggestions.
Here's where my problem is:
private List<Suggestion> suggestions = new ArrayList<>();
public List<Suggestion> searchGames(){
HttpResponse<String> request;
try {
request = Unirest.get("http://giantbomb.com/api/search/?api_key=xxxxxxxxxxxxxxxxxxxxxxxxx&format=json&resources=game&limit=10&field_list=name&query=creed")
.header("Accept", "application/json")
.asString();
String requestString = request.toString(); //I can get my request as String, but that doesnt change much, I guess
//>>> the problematic part <<<
return suggestions;
}
I'm missing something that would allow me to convert JSON response from Unirest to a List. Here's what I tried:
My JSON from Unirest call looks pretty much like this:
{
"error": "OK",
"limit": 10,
"offset": 0,
"number_of_page_results": 1,
"number_of_total_results": 3,
"status_code": 1,
"results": [
{
"name": "Assassin's Creed",
"resource_type": "game"
},
{
"name": "The Creed",
"resource_type": "game"
},
{
"name": "Assassin's Creed: Lost Legacy",
"resource_type": "game"
}
]
}
I have a similar solution for Game class where I display List called games.
private List<Game> games = new ArrayList<>();
public List<Game> getAllGames(){
return games;
}
I populate this list using a GET requests where I enter id and title.
Suggestions coming from Giant Bomb API are trickier for me, since I'm using external API to get a list of objects, instead of entering my input data.
My Suggestion.java class looks like this:
package rest.library;
public class Suggestion {
private long id;
private String name;
public Suggestion(){
}
public Suggestion(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
}
In my Controller I want to do this:
@RequestMapping("/search")
public List<Suggestion> searchGames() { return gameService.searchGames(); }
Going under localhost:8080/search should return a List of Objects with search results gained from JSON request. It should look just like localhost:8080/games that returns me a list of games that I've sent using GET on Postman. I know that I get field "resource_type" from the API and there is no "id" field, but that's a problem for later since I think that filling selected class fields and adding id incrementaly won't be that much of a problem.
What you need is a way to map the String object you received from your request into a HashMap, so that you can then set your object attributes, and form a list to return (even though there are APIs like Jackson which make implementing REST request processing logic much less painful).
This means first getting the InputStream from the HTTP request using response.getEntity().getContent()
, and then creating a JSONParser as mentioned here.
This will enable you to get attributes from the JSON string as shown here.
In turn, this will enable you to use setter methods to create your own object list.
Let me know if that helped at all.