Spring Boot调用Rick and Morty API:单角色接口返回Null问题咨询
Spring Boot调用Rick and Morty API批量正常但单个角色返回Null问题排查
我正在学习Spring Boot调用API,使用Rick and Morty API时,调用获取所有角色的接口可正常返回数据,但将端点修改为获取单个角色(如https://rickandmortyapi.com/api/character/2)时,返回结果为{"info":null,"results":null}。请求解释为何批量获取正常但单个获取返回Null?
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); } }
问题原因
核心问题是批量接口和单个角色接口的返回JSON结构完全不同,你用同一个Character类去接收两种不同结构的响应,导致反序列化失败:
- 批量获取接口(
/api/character)返回的是包含info(分页信息)和results(角色列表)的对象,你的Character类应该是适配这个结构的; - 单个角色接口(
/api/character/2)返回的是单个角色的完整数据,没有info和results字段,直接是角色的属性(如id、name、status等)。用适配批量结构的Character类接收时,因字段不匹配,info和results只能被赋值为null。
解决方案
拆分POJO类:
- 创建
CharacterListResponse类,用于接收批量接口响应,包含Info类型的info字段和List<SingleCharacter>类型的results字段; - 创建
SingleCharacter类,用于接收单个角色接口响应,包含单个角色的所有属性(id、name、status等)。
- 创建
修改Service类:
新增获取单个角色的方法,指定正确的响应类型:public SingleCharacter getSingleCharacter(int characterId) { httpHeaders.setAccept(List.of(MediaType.APPLICATION_JSON)); HttpEntity<String> entity = new HttpEntity<>(httpHeaders); String url = CHAHRACTER_API + "/" + characterId; ResponseEntity<SingleCharacter> response = restTemplate.exchange(url, HttpMethod.GET, entity, SingleCharacter.class); return response.getBody(); }新增Controller接口:
添加带路径参数的接口,用于获取单个角色:@GetMapping("/character/{id}") public ResponseEntity<SingleCharacter> getSingleCharacter(@PathVariable int id) { SingleCharacter character = apiService.getSingleCharacter(id); return new ResponseEntity<>(character, HttpStatus.OK); }
内容的提问来源于stack exchange,提问作者Aldo
相关产品推荐
相关产品推荐

