我正在嘗試了解 Spring Boot 中的 API,并且我正在努力使用 Rick and Morty API,當我提供 getAllCharacters 時,API 會毫無問題地回傳所有字符,但是,例如,當我嘗試只檢索一個字符(使用相同的邏輯,只是更改端點)
服務:
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();
}
}
控制器
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);
}
}
該代碼回傳所有字符,但是,只是為了了解,如果我試圖獲取更改服務類中private static final String CHAHRACTER_API = "https://rickandmortyapi.com/api/character";for的特定字符private static final String CHAHRACTER_API = "https://rickandmortyapi.com/api/character/2";,則回傳 null
{
"info": null,
"results": null
}
?誰能解釋一下為什么可以檢索json的許多元素但不能檢索一個?
謝謝!
uj5u.com熱心網友回復:
獲取所有字符的 api 的 JSON 回應是這樣的結構:
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"
},
]
}
而獲取單個字符的 api 的 JSON 回應具有不同的結構:
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"
}
如您所見"results",單字符 JSON 回應中沒有 Array 物件。因此,您需要相應地閱讀回應。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/530818.html
標籤:爪哇弹簧靴api
