Reputation: 125
I'm seeing an issue with JSON cut-off/missing/incomplete when getting a response back from my service in Spring Boot.
Example:
ResponseEntity<MyDTO> responseEntity = myService.getMyDTO();
return responseEntity;
public class MyService {
public ResponseEntity<MyDTO> getMyDTO() {
return restTemplate.exchange(requestUrl, HttpMethod.GET, new HttpEntity<>(httpHeaders), MyDTO.class)
}
}
When debugging and inspecting the body of ResponseEntity, which is MyDTO instance, it contains all the expected fields.
public class MyDTO {
private Information information;
private Address address;
}
public class Information {
private String firstName;
private String lastName;
}
public class Address {
private String streetName;
}
Debugging:
MyDTO
body
information
firstName > "John"
lastName > "Doe"
address > null
Expected JSON:
{
"information": {
"firstName": "John",
"lastName": "Doe"
},
"address: null
}
Actual JSON:
{
"information": {
"firstName": "Jo
Yes, the ending brackets are even missing from the response JSON.
Note: address is null because the response from upstream service returns a 400 and we map that response to our DTO (MyDTO). Initially, I thought this was the problem until debugging confirmed that the mapping was done correctly.
Here's what's really strange. If I take that ResponseEntity's body and put it in another ResponseEntity, than it returns fine and works. The service even returns faster, which is weird too.
ResponseEntity responseEntity = myService.getMyDTO();
return new ResponseEntity(responseEntity.getBody(), responseEntity.getStatusCode());
Anyone knows what's going on? Is it a networking, Spring Boot, or my code issue? Why would returning a new ResponseEntity fix the issue?
Upvotes: 3
Views: 1922
Reputation: 11
This is happening because you are calling the restTemplate.exchange()
method; if you use the getForObject()
method (in case of post call postForObject()
) instead it will work.
This method will first create the MyDTO class object and then you need to add that object in the ResponseEntity object to return it.
Example:
MyDTO myDtoObject = restTemplate.getForObject(requestUrl, MyDTO.class);
return new ResponseEntity<>(myDtoObject, HttpStatus.OK);
Upvotes: 1
Reputation: 125
Found the issue. The upstream service contains a 'Content-Length' header, which is too small and causing the missing/incomplete JSON on client-side.
Upvotes: 3