spring-projects / spring-projects/spring-hateoas

Returns duplicate links causing JSON deserialization exception

Open
#406 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

process: waiting for feedback
Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

Vanilla resource User -

public class User implements Serializable {
    private static final long serialVersionUID = 262950482349139355L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "FIRST_NAME", nullable = false, unique = false)
    @Convert(converter = NameConverter.class)
    private String firstName;

    @Column(name = "LAST_NAME", nullable = false, unique = false)
    @Convert(converter = NameConverter.class)
    private String lastName;

    @Column(name = "PHONE_NUM", nullable = false, unique = false)
    @Convert(converter = PhoneNumberConverter.class)
    private String phoneNum;

    @Column(name = "EMAIL", nullable = true, unique = false)
    @Convert(converter = OptionalStringConverter.class)
    private Optional<String> email;
}

Search result with hostname changed in the URL - 2 _links present

{
  "_links" : {
    "self" : {
      "href" : "http://hostname/users/search/findByLastName?lastName=doe{&page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "users" : [ {
      "firstName" : "John",
      "lastName" : "Doe",
      "phoneNum" : "111-111-1111",
      "email" : null,
      "_links" : { },
      "_embedded" : { },
      "_links" : {
        "self" : {
          "href" : "http://hostname/users/1",
          "templated" : false
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 1,
    "totalPages" : 1,
    "number" : 0
  }
}

The duplicate links are not present when the resource is accessed on it's own. However, when a client (a microservice in my case) makes a request using a RestTemplate, the response comes back with 2 _links as shown above.

@Override
public Collection<Long> getUserIdsByFirstName(String firstName) {
    String findByFirstNameUri = UriComponentsBuilder.fromUriString(userServiceUrl).path("/users/search/findByFirstName").queryParam("firstName", firstName).toUriString();

    // This is where ResponseEntity.body would have the 2 _links, if ResponseEntity<String> were used. I used a custom object to weed that out.
    ResponseEntity<UserSearchResult> userSearchResult = restTemplate.exchange(findByFirstNameUri, GET, dummyEntity, UserSearchResult.class);
         ...
}

I'm using Spring Data JPA, Spring Data Rest, Spring HATEOAS and Spring Cloud. Here's a link to the project on my Github.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked user-service project, especially the RestTemplate exchange in getUserIdsByFirstName, and reproduce the search response with the shown User resource. Trace Spring HATEOAS serialization or deserialization to determine why duplicate _links appear; done means the response has one valid _links object and can be deserialized without the reported exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.