0

I have a model response that extends RepresentationModel :

@Getter @Setter
public class AddressRestResponseModel extends RepresentationModel<AddressRestResponseModel> {
    private String addressKeyId;
    private String city;
    private String country;
    private String streetName;
    private String zipCode;
}

I also have a controller that uses HateOAS and should return hal+json:

@GetMapping(
        path="/{userKeyId}/addresses",
        produces = {
                MediaType.APPLICATION_XML_VALUE,
                MediaType.APPLICATION_JSON_VALUE,
                "application/hal+json"
        }
)
public List<AddressRestResponseModel> getUserAddresses(@PathVariable String userKeyId) {
    List<AddressRestResponseModel> returnValue = new ArrayList<>();
    List<AddressDto> addressDtos = addressService.getAddresses(userKeyId);
    if (addressDtos != null && !addressDtos.isEmpty()) {
        ModelMapper modelMapper = new ModelMapper();
        Type listType = new TypeToken<List<AddressRestResponseModel>>() {}.getType();
        returnValue = modelMapper.map(addressDtos, listType);

        for (AddressRestResponseModel address: returnValue) {
            Link addressLink = linkTo(methodOn(UserController.class).getUserAddress(userKeyId, address.getAddressKeyId())).withSelfRel();
            address.add(addressLink);

            Link userLink = linkTo(methodOn(UserController.class).getUser(userKeyId)).withRel("user");
            address.add(userLink);
        }
    }
    return returnValue;
}

I also added the pom dependency for hateoas :

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-hateoas -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
    <version>2.2.4.RELEASE</version>
</dependency>

I tried this entry point in postman and set the accept header to "application/hal+json". I should receive something like with "_links" and with objects in it:

Unfortunatelly as response I do not have hal format and it looks like it's not using this format :

[
    {
        "addressKeyId": "0eLoiLVfuAmNIPdUH8rZSGFntYGnRF",
        "city": "Somewhere",
        "country": "Somewhere",
        "streetName": "123, mystreet",
        "zipCode": "12345F",
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8080/appws/users/MxQp8LEUYCELQzmvVx9RpLtWOH1bIY/addresses/0eLoiLVfuAmNIPdUH8rZSGFntYGnRF"
            },
            {
                "rel": "user",
                "href": "http://localhost:8080/appws/users/MxQp8LEUYCELQzmvVx9RpLtWOH1bIY"
            }
        ]
    },...
davidvera
  • 1,292
  • 2
  • 24
  • 55

1 Answers1

0

I just noticed that i do not return the right stuff. I return List instead of CollectionModel

public CollectionModel<AddressRestResponseModel> getUserAddresses(@PathVariable String userKeyId) {
    // same code 
    return new CollectionModel<>(returnValue);
}
davidvera
  • 1,292
  • 2
  • 24
  • 55
  • This is also the reason I would recommend using a `SimpleRepresentationModelAssembler`. It provides the "right" methods where you can define single and multiple model assemblers. Returning them from your controllers will also yield the right outputs. – gregturn Mar 18 '20 at 21:08