Test Rest Template HATEOAS API return empty body but curl get the content

238 Views Asked by At

I make an API with Springboot

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-rest</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
<version>2.7.5</version>
</dependency>
@RepositoryRestResource

When i make a curl or http call in the browser, i get the data (an array of data):

curl -X 'GET' \
  'http://localhost:8083/hospitals?page=0&size=20' \
  -H 'accept: **application/hal+json**'
{
  "_embedded": {
    "hospitals": [
      {
        "latitude": 51.37999725341797,
        "longitude": -0.40604206919670105,
        "webSite": "",
        "parentName": "Virgin Care Services Ltd",
        "email": "",
        "organisationID": 17970,
        "organisationCode": "NDA07",
        "organisationType": null,
        "organisationName": "Walton Community Hospital - Virgin Care Services Ltd",
        "address1": "",
        "address2": "Rodney Road",
        "address3": "",
        "city": "Walton-on-Thames",
        "county": "Surrey",
        "postcode": "KT12 3LD",
        "parentODSCode": "NDA",
        "phone": "01932 414205",
        "fax": "01932 253674",
        "_links": {
          "self": {
            "href": "http://localhost:8083/hospitals/0"
          },
          "hospital": {
            "href": "http://localhost:8083/hospitals/0"
          },
          "specialtiesServices": {
            "href": "http://localhost:8083/hospitals/0/specialtiesServices"
          }
        }
      },
      {
        "latitude": 51.31513214111328,
        "longitude": -0.5562894940376282,
        "webSite": "",
        "parentName": "Virgin Care Services Ltd",
        "email": "",
        "organisationID": 17981,
        "organisationCode": "NDA18",
        "organisationType": null,
        "organisationName": "Woking Community Hospital (Virgin Care)",
        "address1": "",
        "address2": "Heathside Road",
        "address3": "",
        "city": "Woking",
        "county": "Surrey",
        "postcode": "GU22 7HS",
        "parentODSCode": "NDA",
        "phone": "01483 715911",
        "fax": "",
        "_links": {
          "self": {
            "href": "http://localhost:8083/hospitals/2"
          },
          "hospital": {
            "href": "http://localhost:8083/hospitals/2"
          },
          "specialtiesServices": {
            "href": "http://localhost:8083/hospitals/2/specialtiesServices"
          }
        }
      }
    ]
  },
  "_links": {
    "first": {
      "href": "http://localhost:8083/hospitals?page=0&size=2"
    },
    "self": {
      "href": "http://localhost:8083/hospitals?page=0&size=2"
    },
    "next": {
      "href": "http://localhost:8083/hospitals?page=1&size=2"
    },
    "last": {
      "href": "http://localhost:8083/hospitals?page=100&size=2"
    },
    "profile": {
      "href": "http://localhost:8083/profile/hospitals"
    },
    "search": {
      "href": "http://localhost:8083/hospitals/search"
    }
  },
  "page": {
    "size": 2,
    "totalElements": 201,
    "totalPages": 101,
    "number": 0
  }
}

but when i use TestRestTemplate the body is empty. It's work for single object but not for list of object.

ResponseEntity<PagedModel<Hospital>> pageResources;

ParameterizedTypeReference<PagedModel<Hospital>> responseType;

responseType = new ParameterizedTypeReference<PagedModel<Hospital>>() { };

pageResources = initResttemplate().exchange(getUrl(arg0),HttpMethod.GET,HttpEntity.EMPTY, responseType);

Assertions.assertNotNull(pageResources.getBody());

This is the content of the response in debug mode.

PagedModel { content: [], fallbackType: null, metadata: Metadata { number: 0, total pages: 11, total elements: 201, size: 20 }, links:  }

What type of object i need to use for consume my list of object ?

1

There are 1 best solutions below

0
MichaelK On

I had the same problem before.

I read some more comments and documentation and found this solution for me.

You have to register the hypermedia support for the rest template.

Now a small code snippet adapted to your code.

public class TestClass {

    @Autowired
    public HypermediaRestTemplateConfigurer configurer;
    
    public void myMethod(){

        ResponseEntity<PagedModel<Hospital>> pageResources;
            
        ParameterizedTypeReference<PagedModel<Hospital>> responseType;
            
        responseType = new ParameterizedTypeReference<PagedModel<Hospital>>() { };
        //configurer.registerHypermediaTypes() arround the RestTemplate not RestTestTemplate, else RestTestTemplate.getRestTemplate()    
        pageResources =  configurer.registerHypermediaTypes(initResttemplate()).exchange(getUrl(arg0),HttpMethod.GET,HttpEntity.EMPTY, responseType);
            
        Assertions.assertNotNull(pageResources.getBody());
        
        }

}

Sometimes you need an extra annotation. But I did not need it in my case. Here is the link if the code above does not work completely:

Spring HATEOS : Consider defining a bean of type 'org.springframework.hateoas.config.HypermediaRestTemplateConfigurer' in your configuration