trap
trap

Reputation: 2640

How to access data from RestTemplate as List

I want to access data from a spring boot service. The return type of the data is a List, but every time I access it, the list is empty.

This is my code:

Map<String, String> params = new HashMap<String, String>();
params.put("firstName", "test" );
params.put("lastName", "test1");
ResponseEntity<Person[]> response = restTemplate.getForEntity(url, Person[].class, params);

In this case, response.getBody() is an empty [].

@RequestMapping(value = "/search", method = RequestMethod.GET)
public List<Person> searchUsers(
        @RequestParam(value = "firstName", required = false) String firstName,
        @RequestParam(value = "lastName", required = false) String lastName,
        @RequestParam(value = "email", required = false) String email {

    return personService.search(firstName, lastName, email, company);
}

I also tried with String, and Person[], but nothing worked.

Thanks in advance!

Upvotes: 0

Views: 170

Answers (1)

samirtk8
samirtk8

Reputation: 153

@GET
@Path("statement")
@Produces({MediaType.APPLICATION_XML})
public Response statement(@QueryParam("from") String from, @QueryParam("to") String to) {
    DB idb = new DB();
    List<Transaction> transactions = idb.getTransactionsByDate(from, to);
    final GenericEntity<List<Transaction>> entity = new GenericEntity<List<Transaction>>(transactions) {
    };
    return Response.status(Response.Status.OK).entity(entity).build();
}

Upvotes: 1

Related Questions