ResponseEntity返回java.lang.Object

时间:2016-06-30 07:25:29

标签: java json rest controller resttemplate

我有这个服务从数据库中获取某些用户并将它们作为json对象返回。
我想收到响应并将其用作json但我几乎100%确定我做错了因为如果我打印响应我得到了这个:

[Ljava.lang.Object;@69a38065


这是控制器:

    public Object[] getClienteNomeCognome(String nome, String cognome) throws Exception {
    try {
        final RestTemplate restTemplate = new RestTemplate();
        final String url = "somelink?cognome=%25"+cognome+"%25&nome=%25"+nome+"%25";
        //final ResponseEntity<List> response = (ResponseEntity<List>) restTemplate.getForObject(url, List.class);
        final ResponseEntity<Object[]> response = restTemplate.getForEntity(url, Object[].class);
        Object[] risultati = response.getBody();
        LOGGER.info("La richiesta e' stata effettuata con status code: " + response.getStatusCode().toString());
        if (risultati != null && risultati.toString().contains("<error>")) {
            throw new Exception(String.format(
                    "[SERVICE] La risposta del servizio contiene degli errori: %s",
                    risultati));
        } else {
            LOGGER.debug("[SERVICE] Fine chiamata al servizio di ricerca cliente");
            LOGGER.info(response.getBody().toString());
            return risultati;
        }
    } catch (HttpClientErrorException hcee) {
        throw new Exception(String.format(
                "[SERVICE] Errore durante la chiamata. Error: %s",
                hcee.getMessage()));
    } catch (Exception e) {
        throw new Exception(String.format(
                "[SERVICE] Errore generico durante la chiamata al servizio. Error: %s",
                        e.getMessage()));
    }

}

1 个答案:

答案 0 :(得分:0)

您正在显示toString()结果,该结果尚未在Object类上重载。因此,它显示对象的名称和地址。

您应该使用类来存储结果,并且可以重载toString()以获得有意义的表示。

您还可以使用强类型列表来显示结果:

ResponseEntity<List<YourEntity>> yourResponse = restTemplate.exchange("https://some-link/your-object-location",
                HttpMethod.GET, null, new ParameterizedTypeReference<List<YourEntity>>() {
        });
List<YourEntity> entities = yourResponse .getBody();