使用RestTemplate在Spring Data Rest Response中缺少链接

时间:2016-09-18 10:25:05

标签: spring spring-boot spring-data-rest spring-hateoas

当我调用Spring Data Rest Endpoint时,我希望看到每个对象中的自我链接和相关链接。没有链接出现。

RestTemplate设置:

@HystrixCommand(fallbackMethod = "getFallbackScenicList")
@RequestMapping(value = "/s", method = RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)
public PagedResources<Scenic> scenic() {
    String url = "http://vr-dms-an-scenic/scenic";
    ParameterizedTypeReference<PagedResources<Scenic>> ptr = new ParameterizedTypeReference<PagedResources<Scenic>>() {};

    ResponseEntity<PagedResources<Scenic>> responseEntity =
        this.restTemplate.exchange(url,HttpMethod.GET, null, ptr, 0,100
        );

    PagedResources<Scenic> resources = responseEntity.getBody();

    return resources;
}

预期回应:

{
    "_embedded": {
        "scenic": [
            {
                "id": 1,
                "name": "Test1 scenic",
                "description": "This is a description1 for displaying information while in development",
                "shortDescription": "Short Description Scenic1",
                "_links": {
                    "self": {
                        "href": "http://localhost:49218/scenic/1"
                    },
                    "scenic": {
                        "href": "http://localhost:49218/scenic/1"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:49218/scenic"
        },
        "profile": {
            "href": "http://localhost:49218/profile/scenic"
        },
        "search": {
            "href": "http://localhost:49218/scenic/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}

实际回应:

{
    "_embedded": {
        "scenic": [
            {
                "id": 1,
                "name": "Test1 scenic",
                "description": "This is a description1 for displaying information while in development",
                "shortDescription": "Short Description Scenic1"
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:49218/scenic"
        },
        "profile": {
            "href": "http://localhost:49218/profile/scenic"
        },
        "search": {
            "href": "http://localhost:49218/scenic/search"
        }
    },
    "page": {
        "size": 20,
        "totalElements": 1,
        "totalPages": 1,
        "number": 0
    }
}

1 个答案:

答案 0 :(得分:2)

我认为Scenic不包含链接。而不是

PagedResources<Scenic>

你真的想要

PagedResources<Resource<Scenic>>