1

On one side, I have just update the version of spring-data-rest-webmc to the latest 2.0.0.RC1 version of my server. In this version, the json format change to an HAL format.

On the other side, I have a client which use the spring-hateoas library with the 0.9.0.RELEASE version. In this client, I use RestTemplate to get a resource from my server like this :

AuthorResource authorResource =  restTemplate.getForObject(BASE_URL+"authors/"+ authorId, AuthorResource.class);

The AuthorResource class extends ResourceSupport.

Now, I have this error :

Nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "_links" (class org.example.hateoas.AuthorResource)

How can i configure my client to support this new format ? I try

@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)

But it does not work.

thx for your help.

bwilcox
  • 629
  • 1
  • 6
  • 14

1 Answers1

3

Problem is that halMapperObject is not setted because of context is not spring web. You have to create your own RestTemplate class like this

@Component
public class EraRestTemplate extends RestTemplate implements InitializingBean {

@Autowired
@Qualifier("_halObjectMapper")
ObjectMapper halObjectMapper;

static class HALMessageConverter extends MappingJackson2HttpMessageConverter {

}

@Override
public void afterPropertiesSet() throws Exception {
    halObjectMapper.registerModule(new Jackson2HalModule());

    HALMessageConverter converter = new HALMessageConverter();
    converter.setObjectMapper(halObjectMapper);

    this.getMessageConverters().clear();
    this.getMessageConverters().add(converter);
}
}

It works fine now for me thanks a friend who knows Spring very well.

Howli
  • 12,291
  • 19
  • 47
  • 72
gjouanjan
  • 31
  • 2