I'm trying to parse the JSON from Github's commit details API into a HashMap. The Sample Json I've used in my tests is here
The test code is
@SpringJUnitConfig(classes = {GithubApiService.class, RestTemplateConfiguration.class})
@EnableAutoConfiguration
public class GithubApiServiceTest {
@Test
public void testGithubResponseJsonToMapConversion(
@Autowired RestTemplate restTemplate,
@Autowired GithubApiService service,
@Value("classpath:github/commit-payload.json") Resource commitPayloadFile) throws IOException {
final String COMMITS_URL = "https://api.github.com/repos/Codertocat/Hello-World/commits/sha";
//Stub response from Github Server
String responseJson = new ObjectMapper().writeValueAsString(
new String(Files.readAllBytes(Paths.get(commitPayloadFile.getFile().getAbsolutePath()))));
MockRestServiceServer mockGithubServer = MockRestServiceServer.createServer(restTemplate);
mockGithubServer.expect(requestTo(COMMITS_URL))
.andRespond(withSuccess().contentType(APPLICATION_JSON).body(responseJson));
//Call API Service
Map<String, Object> result = service.getCommitDetails(COMMITS_URL);
//Expect return type is hashmap
assertThat(result.get("sha")).isEqualTo("6dcb09b5b57875f334f61aebed695e2e4193db5e");
}
}
The Service code is
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class GithubApiService {
@Autowired
private final RestTemplate restTemplate;
public Map<String, Object> getCommitDetails(String commitsUrl) {
ParameterizedTypeReference<Map<String, Object>> responseType = new ParameterizedTypeReference<Map<String, Object>>() {
};
RequestEntity<Void> request = RequestEntity.get(URI.create(commitsUrl)).accept(APPLICATION_JSON).build();
return restTemplate.exchange(request, responseType).getBody();
}
}
This fails converting the JSON
response into a Map
with the following error (full log here)
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `java.util.LinkedHashMap` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{
"url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==",
"html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
"comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments",
Since I'm using spring-boot-starter-web
it auto-wires converters including MappingJackson2HttpMessageConverter
but debugger shows that the response is being processed by the ByteArrayHttpMessageConverter
despite setting content type to application/json
.
My bad, I should have used the @RestClientTest
annotation intead of @EnableAutoConfiguration
, that fixed the problem.
@EnableAutoConfiguration
is a more generic annotation which doesn't achieve all the auto config that @RestClientTest
does