简体   繁体   English

Spring RestTemplate getForObject Map<string, pojo> class 映射</string,>

[英]Spring RestTemplate getForObject Map<String, POJO> class mapping

I have the following POJO:我有以下 POJO:

class TestPojo {

    private Long testId = null;
    private String testString = null;
    private boolean testBoolean = false;
    private Float testFloat = null;
    private int testInt = 0;
    private Collection<String> testCollection = new TreeSet();

    Long getTestId() {
        return testId;
    }

    void setTestId(Long testId) {
        this.testId = testId;
    }

    String getTestString() {
        return testString;
    }

    void setTestString(String testString) {
        this.testString = testString;
    }

    boolean getTestBoolean() {
        return testBoolean;
    }

    void setTestBoolean(boolean testBoolean) {
        this.testBoolean = testBoolean;
    }

    Float getTestFloat() {
        return testFloat;
    }

    void setTestFloat(Float testFloat) {
        this.testFloat = testFloat;
    }

    int getTestInt() {
        return testInt;
    }

    void setTestInt(int testInt) {
        this.testInt = testInt;
    }

    Collection<String> getTestCollection() {
        return testCollection;
    }

    void setTestCollection(Collection<String> testCollection) {
        this.testCollection = testCollection;
    }
}

I have two rest API responses, one which returns a single instance of TestPojo in JSON format:我有两个 rest API 响应,一个返回 JSON 格式的单个 TestPojo 实例:

{
  "testId": 1,
  "testString": "testStringA",
  "testBoolean": false,
  "testFloat": 1.0,
  "testInt": 1,
  "testCollection": [
    "testCollectionA",
    "testCollectionB"
  ]
}

The other rest API response returns a map of multiple TestPojo's:另一个 rest API 响应返回多个 TestPojo 的 map:

{
  "TestPojoA": {
    "testId": 1,
    "testString": "testStringA",
    "testBoolean": false,
    "testFloat": 1.0,
    "testInt": 1,
    "testCollection": [
      "testCollectionA",
      "testCollectionB"
    ]
  },
  "TestPojoB": {
    "testId": 2,
    "testString": "testStringB",
    "testBoolean": true,
    "testFloat": 1.1,
    "testInt": 2,
    "testCollection": [
      "testCollectionC",
      "testCollectionB"
    ]
  }
}

My Rest Template with MappingJackson2HttpMessageConverter correctly returns the POJO for the first JSON:我的 Rest 模板与 MappingJackson2HttpMessageConverter 正确返回第一个 JSON 的 POJO:

RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
TestPojo testPojo = restTemplate.getForObject("http://127.0.0.1:8081/test/getPojoInstance", TestPojo.class);

I can't get the POJO in the map to correctly convert though however?但是,我无法让 map 中的 POJO 正确转换? I've tried various ways, I thought the following would work but this just returns a map with two string values instead of String & POJO:我尝试了各种方法,我认为以下方法可行,但这只是返回一个 map,其中包含两个字符串值,而不是 String 和 POJO:

RestTemplate restTemplate = new RestTemplate();
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
Map<String, TestPojo> testPojoMap = restTemplate.getForObject("http://127.0.0.1:8081/test/getPojoMap", Map<String, TestPojo>.class);

I know I can convert the string value in my map to a TestPojo after I receive the response but I'm just curious if it's something I'm not doing correctly in the first instance?我知道我可以在收到响应后将 map 中的字符串值转换为 TestPojo 但我只是好奇这是不是我一开始做的不对?

RestTemplate can deserialize JSON like you have into a java.util.Map not into java.util.Map<String, TestPojo> RestTemplate can deserialize JSON like you have into a java.util.Map not into java.util.Map<String, TestPojo>

You can use Jackson ObjectMapper to give TypeReference您可以使用Jackson ObjectMapper给 TypeReference

String testPojoMap = restTemplate.getForObject("http://127.0.0.1:8081/test/getPojoMap", String.class);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, TestPojo> map = objectMapper.readValue(testPojoMap, new TypeReference<Map<String,TestPojo>>(){});

Or或者

Without Rest templete不带 Rest 模板

ObjectMapper objectMapper = new ObjectMapper();
Map<String, TestPojo> map = objectMapper.readValue("http://127.0.0.1:8081/test/getPojoMap", new TypeReference<Map<String,TestPojo>>(){});

I think it was because of type problem in getForObject , I would suggest to use exchange method我认为这是由于getForObject中的类型问题,我建议使用交换方法

ResponseEntity<Map<String, TestPojo>> response = template.exchange("http://127.0.0.1:8081/test/getPojoMap", 
         HttpMethod.GET, null, new ParameterizedTypeReference<Map<String, TestPojo>>() {
});

Map<String, TestPojo> body = response.getBody();

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

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