简体   繁体   English

Spring RestTemplate - ResponseBody类的外观如何

[英]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: 我有一个非常简单的休息客户端,我想将服务响应解析为自定义的ResponseBody类:

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: 实际问题是CustomResponseBody类应该是什么样子,就setter,getters和构造函数而言,假设我正在使用的服务的输出是JSON,如:

{item1:something, item2:otherthing}

And my makeRestTemplate method is: 我的makeRestTemplate方法是:

    @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. 我使用了lombok的anotation @Data,但我想知道customResponseBody类实际解析响应的最低要求是什么。

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. 一个简单的POJO完成了这项工作,我在尝试实现自定义setter和getter方法时遇到了一些问题,它看起来像普通getter和setter方法旁边的任何东西都会使绑定过程失败。

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. 如果json是{“item1”:value1,“item2”:value2}而你只想要这两个值,那么你的映射pojo应该有两个字段,“item1”和“item2”,其类型与value1和value2相同。 Default setters and getters should be fine. 默认的setter和getter应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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