javajax-rsdropwizard

javax.ws.rs.ProcessingException: Error reading entity from input stream while reading List of an Object in Java


I am using Dropwizard. I am trying to use Response to read a class that returns a list of an Object. I have tried using Generic Type and read comments of people who have similar issues but I still have the same error message on hitting the endpoint.

This is the method that calls the api.

public List<InactiveAgentsDto> fetchAgentsUnderAggregator(String aggregatorId, String token) throws RecordNotFoundException, IOException {
        String url = this.baseUrl+"/api/v1/users/aggregator/agent/" + aggregatorId;
        Response response = client.target(url).request().header(HttpHeaders.AUTHORIZATION, token).get();
        
        if(response.getStatus() == 200){
            List<InactiveAgentsDto> allAgents = response.readEntity(new GenericType<List<InactiveAgentsDto>>(){});
            return allAgents;
        }
        throw new RecordNotFoundException(response.getStatusInfo().getReasonPhrase());
    }

This is the error that I have.

javax.ws.rs.ProcessingException: Error reading entity from input stream

I have also tried this

 return response.readEntity(new ArrayList<InactiveAgentsDto>().getClass());

I get the same error message as above.

on logging String.class, I get the desired data that I need.

log.info("This is the error message {}", response.readEntity(String.class)); 

This prints the list of data to the console. Still wondering what I am doing wrong.

This is the content of inactiveAgentsDTO

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public classInactiveAgentsDto {

    private String agentId;
    private String firstName;
    private String lastName;
    private String state;
    private String aggregatorId;
    private String phoneNumber;
    private BigDecimal cashOutAmount;
    private long cashOutCount;
    private Types.AgentTransactionStatus status;
}

I have taken out the imports.


Solution

  • This is the fix. I created a new class that contains a List of inactive agents. The class is called AllAgentsInfo.

    public class AllAgentsInfo {
        private List<InactiveAgentsDto> data;
    }
    

    Then I had to rewrite the method to this

    public AllAgentsInfo fetchAgentsUnderAggregator(String aggregatorId, String token) throws RecordNotFoundException,
                IOException {
            String url = this.baseUrl+"/api/v1/users/aggregator/agent/" + aggregatorId;
            Response response = client.target(url).request().header(HttpHeaders.AUTHORIZATION, token).get();
    
            String stringEntity = response.readEntity(String.class);
    
            ResponseDto responseDto  = new ObjectMapper().readValue(stringEntity, ResponseDto.class);
    
            if(Objects.isNull(responseDto) || !responseDto.isStatus()){
                String message = Objects.isNull(responseDto) || Objects.isNull(responseDto.getMessage()) ? "Unable to get agent information" : responseDto.getMessage();
                throw new InternalServerErrorException(message);
            }
            return  new ObjectMapper().convertValue(responseDto, AllAgentsInfo.class);
    
        }
    

    This fixed my problem. In case you are wondering what the ResponseDTO.

    public class ResponseDto<T> {
        private String message;
        private String responseCode;
        private boolean status;
        private T data;
    
        public ResponseDto(String message, String responseCode, boolean status, T data) {
            this.message = message;
            this.responseCode = responseCode;
            this.status = status;
            this.data = data;
        }
    }