I have a basic java webserver for persisting payment history of users. I am trying to write a method to return a list of payments by querying a database, but for some reason the client keeps throwing an exception.
Here is my resource method for the server:
@RolesAllowed({"authenticated","administrator","superadministrator"})
@Path("getPaymentHistory/{userId}")
@GET
public Response getPaymentHistory(@Context SecurityContext sc, @PathParam("userId") String userId){
PaymentListResponse response = paymentService.getUserPaymentHistory(userId);
logger.debug("payment service found " +response.getPayments().size()+ " payments for user: " + userId);
return Response.ok().entity(response).build();
}
This produces the following log :
DEBUG c.b.s.p.resource.PaymentResource - payment service found 3 payments for user: c832f8c2-f5ff-4e5c-a1b9-7e5a3f26a359
Therefore the list is definitely populated with the 3 payments the user has made previously. I added the list to an XML root element as I thought it may be the cause of the exception:
@XmlRootElement
public class PaymentListResponse {
private List<Payment> payments = new ArrayList<Payment>();
public PaymentListResponse(){}
//getter & setter ..
}
Here is my client code:
DefaultHttpClient httpClient = new DefaultHttpClient();
String responseString = null;
HttpResponse response;
try {
response = httpClient.execute(data);
HttpEntity entity = response.getEntity();
responseString = EntityUtils.toString(entity, "UTF-8");
} catch (HttpException | IOException e) {
e.printStackTrace();
}
return responseString;
This produces the following log:
org.apache.http.MalformedChunkCodingException: Chunked stream ended unexpectedly at org.apache.http.impl.io.ChunkedInputStream.getChunkSize(ChunkedInputStream.java:222) at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream.java:183) at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:155) at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.Reader.read(Reader.java:140) at org.apache.http.util.EntityUtils.toString(EntityUtils.java:135)
I cant figure out why my JSON is causing errors, I have responses from the same Resource being sent back without errors. this happens only when i try to send a populated list. If the list is empty the response from the server is
{"payments":[]}
Update
I used the following code as suggested in the answers:
return Response.ok().entity(response.toArray()).build();
Class annotations are as follows:
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_JSON})
with the same exception being thrown. For clarity, I have used this type of response before in another part of the server. The response from that is also a JSON list and is handled by the same client code.
Here is another method which works:
@RolesAllowed({"administrator","superadministrator"})
@Path("get_all")
@GET
public Response getAccounts(@Context SecurityContext sc) {
ExternalUser userMakingRequest = (ExternalUser)sc.getUserPrincipal();
List<Account> accounts = accountService.getAllAccounts(userMakingRequest);
return Response.ok().entity(accounts).build();
}