Home > Mobile >  Consuming Rick and Morty's API in Spring Boot returns Null
Consuming Rick and Morty's API in Spring Boot returns Null

Time:11-09

I'm trying to learn about API's in Spring Boot, and I'm struggling with the Rick and Morty API, when I give the getAllCharacters, the API returns all of the Characters with no problem, but, for example, when I try to retrieve just one Character (with the same logic, just changing the endpoint)

Service:

package com.burakkutbay.springbootresttemplateexample.service;

import com.burakkutbay.springbootresttemplateexample.pojo.Character;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class ApiServiceImpl implements ApiService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HttpHeaders httpHeaders;

    private static final String CHAHRACTER_API = "https://rickandmortyapi.com/api/character";

    @Override
    public Character getAllCharacter() {

        httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON));

        HttpEntity<String> entity = new HttpEntity<>(httpHeaders);

        ResponseEntity<Character> response = restTemplate.exchange(CHAHRACTER_API, HttpMethod.GET,
                entity, Character.class);

        return response.getBody();
    }
}

Controller

package com.burakkutbay.springbootresttemplateexample.controller;

import com.burakkutbay.springbootresttemplateexample.pojo.Character;
import com.burakkutbay.springbootresttemplateexample.service.ApiService;
import com.burakkutbay.springbootresttemplateexample.service.ApiServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping("/")
    public ResponseEntity<Character> getCharacters() {
        Character characters = apiService.getAllCharacter();
        return new ResponseEntity<>(characters, HttpStatus.OK);
    }
}

That code returns all the Characters, but, just to learn, if I tried to get to a specific character changing the private static final String CHAHRACTER_API = "https://rickandmortyapi.com/api/character"; for private static final String CHAHRACTER_API = "https://rickandmortyapi.com/api/character/2"; in the service class, returns null

{
    "info": null,
    "results": null
}

¿Can anyone please explain why can retrieve many elements of the json but not one?

Thanks!

CodePudding user response:

The JSON response of the api to get all characters is in this structure:

https://rickandmortyapi.com/api/character

{
    "info": {
    "count": 826,
    "pages": 42,
    "next": "https://rickandmortyapi.com/api/character/?page=2",
    "prev": null
    }
    "results": [
    {
      "id": 1,
      "name": "Rick Sanchez",
      "status": "Alive",
      "species": "Human",
      "type": "",
      "gender": "Male",
      "origin": {
        "name": "Earth",
        "url": "https://rickandmortyapi.com/api/location/1"
      },
      "location": {
        "name": "Earth",
        "url": "https://rickandmortyapi.com/api/location/20"
      },
      "image": "https://rickandmortyapi.com/api/character/avatar/1.jpeg",
      "episode": [
        "https://rickandmortyapi.com/api/episode/1",
        "https://rickandmortyapi.com/api/episode/2",
        // ...
      ],
      "url": "https://rickandmortyapi.com/api/character/1",
      "created": "2017-11-04T18:48:46.250Z"
    },
   ]
}

Whereas the JSON response of the api to get a single character has different structure:

https://rickandmortyapi.com/api/character/2

{
  "id": 2,
  "name": "Morty Smith",
  "status": "Alive",
  "species": "Human",
  "type": "",
  "gender": "Male",
  "origin": {
    "name": "Earth",
    "url": "https://rickandmortyapi.com/api/location/1"
  },
  "location": {
    "name": "Earth",
    "url": "https://rickandmortyapi.com/api/location/20"
  },
  "image": "https://rickandmortyapi.com/api/character/avatar/2.jpeg",
  "episode": [
    "https://rickandmortyapi.com/api/episode/1",
    "https://rickandmortyapi.com/api/episode/2",
    // ...
  ],
  "url": "https://rickandmortyapi.com/api/character/2",
  "created": "2017-11-04T18:50:21.651Z"
}

As you can see there is no "results" Array object in the single-character JSON response. So, you need to read the response accordingly.

  • Related