如何使用restemplate使用springhateoas格式的输出(application/json)

ac1kyiln  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(285)

我目前正在处理spring应用程序客户机,它使用来自第三方api的application/json响应。
json:(我知道这是springhateoas表示的形式,我得到的响应头是“application/json,所以我也想探索traverson,但由于它不是hal+json,所以不能工作”)

{
    "links": {
        "next": {
            "href": "/v1/names?page=1&size=2"
        },
        "self": {
            "href": "/v1/names"
        }
    },
    "data": [
        {
            "id": "5960c4ec",
            "name": "1232323",
            "data": {
                "dataid": "cdsc",
                "dataname": "csdc",
                "some": null
            },
            "references": [
                {
                    "id": "...."
                }
            ]
        },
        {
            "id": "34543535",
            "name": "1232323",
            "data": {
                "dataid": "ncsdcs",
                "dataname": "cdscsdc",
                "some": null
            },
            "references": [
                {
                    "id": "csdc"
                }
            ]
        }
    ]
}

Spring Data 类:

public class Data extends RepresentationModel<Data>{
    private String name;
    private String id;
..getter setter
}

try1:响应不包含任何结果

HttpHeaders header = new HttpHeaders();
        header.add("Authorization", "basic ...");
        HttpEntity<Data> httpEntity = new HttpEntity<>(header);
ResponseEntity<CollectionModel<EntityModel<Data>>> responseEntity = template.exchange(
                new URI(akb), HttpMethod.GET, httpEntity,
                new ParameterizedTypeReference<CollectionModel<EntityModel<Data>>>() {});

try2:响应不包含任何结果

HttpHeaders header = new HttpHeaders();
        header.add("Authorization", "basic ...");
        HttpEntity<Data> httpEntity = new HttpEntity<>(header);
ResponseEntity<CollectionModel<PagedModel<Data>>> responseEntity = template.exchange(
                new URI(akb), HttpMethod.GET, httpEntity,
                new ParameterizedTypeReference<CollectionModel<PagedModel<Data>>>() {});

try3:响应不包含任何结果

HttpHeaders header = new HttpHeaders();
        header.add("Authorization", "basic ...");
        HttpEntity<Data> httpEntity = new HttpEntity<>(header);
ResponseEntity<PagedModel<Data>> responseEntity = template.exchange(
                new URI(akb), HttpMethod.GET, httpEntity,
                new ParameterizedTypeReference<PagedModel<Data>>() {});

我仍然可以使用对象的responseentity并迭代结果,但是使用hal形式的json最简单和最好的方法是什么。
我的应用程序中也有resttemplate配置:

@Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.registerModule(new Jackson2HalModule());

        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON,MediaTypes.HAL_JSON,MediaType.ALL));
        converter.setObjectMapper(mapper);

        return builder.messageConverters(converter).build();
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题