Skip to content

Different name for hypermedia links attribute in json response after using RestTemplateBuilder for the first time (links vs _links) #2401

@Fachher

Description

@Fachher

Title

Inconsistent JSON link attribute name (“links” vs “_links”) after introducing RestTemplateBuilder configuration

Description

We are maintaining a legacy application that exposes several REST endpoints. The project uses both spring-boot-starter-hateoas and spring-boot-starter-web.

Existing client applications expect the JSON representation of hypermedia links to appear under a field named links. This naming is part of an established API contract we must preserve.

Up to now, our REST endpoints produced responses containing "links": { ... }" — which was the expected behavior.

However, after we introduced a RestTemplate bean created via RestTemplateBuilder, the behavior changed:

  • the hypermedia links are now serialized as "_links" (HAL style)
  • no other change besides the RestTemplate configuration was made
  • this affects the responses of our controllers, not the RestTemplate itself

To preserve the legacy behavior, we attempted:

spring.hateoas.use-hal-as-default-json-media-type: false

…but it had no effect.

During debugging we noticed that only when the RestTemplate bean is present the bean HypermediaRestTemplateConfigurer appears in the application context.
This bean seems to activate HAL-style configuration globally, which we did not expect — especially since configuring a RestTemplate should not influence how controller responses are serialized.

What we expected

Configuring a custom RestTemplate should not change the JSON serialization format of the server-side HATEOAS responses.

What actually happened

After adding:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
}

our server endpoints start returning HAL-style JSON with "_links" instead of "links".

Minimal Reproducible Example

A minimal project demonstrating the issue is included. It contains two tests:

  • WithoutRestConfigurationTest → produces "links" (expected)
  • WithRestConfigurationTest → produces "_links" (unexpected)

The logs also show that only in the second case HypermediaRestTemplateConfigurer is created.


Minimal reproducible example
https://github.com/Fachher/spring-resttemplate-incident

A quick fix would be to exclude HypermediaAutoConfiguration but this is strange.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions