0

I have the problem similar to one asked in this question however, applying the suggested solution

spring.jackson.default-property-inclusion=NON_NULL does not stop HATEOAS from rendering links with null properties. Here's my controller declaration

@RestController
@ExposesResourceFor(Customer.class)
public class CustomerController {
  // controller methods here
}

and the web config class

@Configuration
@EnableSpringDataWebSupport
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class DataApiWebConfiguration extends WebMvcConfigurationSupport {
  // config here
}

In the Controller get method that returns a resource I declare mapping as follows

@GetMapping(value = "/customers/{id}", produces = MediaTypes.HAL_JSON_VALUE)

and then I return a Resource

Optinal<Customer> customer = customerRepository.findById(id);
return customer.map(customerResourceAssembler::toResource).map(ResponseEntity::ok)
                            .orElse(ResponseEntity.notFound().build());

The CustomerResourceAssembler extends SimpleIdentifiableResourceAssembler as demonstrated in this spring-hateaos example.

But in the response body I still see links rendered with null properties

"links": [
            {
                "rel": "self",
                "href": "http://localhost:8080/customers/11",
                "hreflang": null,
                "media": null,
                "title": null,
                "type": null,
                "deprecation": null
            }
]

this doesn't look like how a HATEOAS response should be, like in examples I see _links not links in the JSON

Anadi Misra
  • 1,925
  • 4
  • 39
  • 68

1 Answers1

0

The problem was in my configuration class, I was registering the Hibernate5Module in a wrong way, removed these lines

@Bean
    public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new HibernateAwareObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        jsonConverter.setObjectMapper(objectMapper);
        return jsonConverter;
    }

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(customJackson2HttpMessageConverter());
    super.addDefaultHttpMessageConverters(converters);
}

and simply added a bean

@Bean
public Module hibernate5Module() {
    return new Hibernate5Module();
}

that fixed the output

{
    "_embedded": {
        "customers": [
            {
                "name": "Minty And Sons Pvt. Ltd.",
                "pan": "5GB7W15M0T",
                "currecny": "INR",
                "tds": 0.1,
                "invoice_prefix": "INV",
                "_links": {
                    "self": {
                        "href": "/customers/1"
                    },
                    "customers": {
                        "href": "/customers"
                    },
                    "contact": {
                        "href": "/customers/1/contact"
                    },
                    "branches": {
                        "href": "/customers/1/branches"
                    },
                    "invoices": {
                        "href": "/customers/1/invoices"
                    },
                    "paid-invoices": {
                        "href": "/customers/1/invoices/paid"
                    },
                    "pending-invoices": {
                        "href": "/customers/1/invoices/pending"
                    },
                    "overdue-invoices": {
                        "href": "/customers/1/invoices/overdue"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/data/api/customers"
        }
    },
    "page": {
        "size": 20,
        "total_elements": 1,
        "total_pages": 1,
        "number": 0
    }
}
Anadi Misra
  • 1,925
  • 4
  • 39
  • 68