简体   繁体   中英

Spring RestTemplate - What does a ResponseBody Class look like

I have a really simple rest client and I would like to parse the service response into a custom ResponseBody Class:

ResponseEntity<CustomResponseBody> entity = restTemplate.postForEntity(uri, request, CustomResponseBody.class);

The actual question is how the CustomResponseBody class should look like, in terms of setters, getters and constructors assuming that the output from the service I am consuming is a JSON like:

{item1:something, item2:otherthing}

And my makeRestTemplate method is:

    @Bean
    public RestTemplate makeRestTemplate() {
    RestTemplate restTemplate = new RestTemplate();

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setConnectTimeout(TIMEOUT_MILLIS);
    requestFactory.setReadTimeout(TIMEOUT_MILLIS);
    restTemplate.setRequestFactory(requestFactory);
    restTemplate.setErrorHandler(errorHandler);

    List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
    for (HttpMessageConverter<?> converter : converters) {
        if (converter instanceof MappingJackson2HttpMessageConverter) {
            MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
            ObjectMapper objectMapper = makeObjectMapper();
            jsonConverter.setObjectMapper(objectMapper);
            jsonConverter.setSupportedMediaTypes(Arrays.asList(new MediaType("application", "json", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET),
                    new MediaType("text", "javascript", MappingJackson2HttpMessageConverter.DEFAULT_CHARSET)));
        }
    }

    return restTemplate;
}

I had it working with the lombok's anotation @Data but I would like to know what is the minimal requirement of the customResponseBody Class to actually parse the response.

UPDATE:

A simple POJO does the job, I had some problems trying to implement custom setter and getter methods, it looks like anything beside plain getter and setter methods makes the binding process fail.

Accepted

import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
public class CustomResponseBody {
    private String item1;
    private String item2;
}

Unaccepted

import lombok.Setter;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class CustomResponseBody {

    @Getter
    @Setter
    private String item1;

    @Getter
    private String item2;

    public void setItem2(String item2) {
        if ("something".equals(item2)) {
            this.item2 = "whatever";
        } else {
            this.item2 = "otherstuff"
        }
    }
}

它应该是简单的POJO实现Serializable,预启用的HttpMessageConverters应该能够根据你的标题中的Content-Type转换任何Serializable POJO。

If the json is {"item1":value1,"item2":value2} and you just want the two values, then your mapping pojo should have two fields, "item1" and "item2" with the same types as value1 and value2. Default setters and getters should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM