spring-projects / spring-projects/spring-hateoas

spring boot hypermedia convert child elements to Array instead of Object

Open
#982 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.1k
Forks
476
PR merge metrics
No merged PRs in 30d

Description

I am using spring-boot-1.5.8 and spring-boot-hateoas in my project. Please find the code below

@SpringBootApplication
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class SpringBootK8Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootK8Application.class, args);
    }
}

Controller

 @GetMapping("/greeting")
    public ResponseEntity<Greeting> greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        Greeting greeting = new Greeting(String.format("Hello, %s!", name));
        greeting.add(linkTo(methodOn(HelloController.class).greeting(name)).withSelfRel());
        Link link = new Link("http://localhost:8080/something").withRel("ACTIONS");
        SuperLink superLink = new SuperLink(link, MediaTypes.HAL_JSON_VALUE,"GET","/v1/books","v1/bookBy");
        greeting.add(superLink);
        return new ResponseEntity<>(greeting, HttpStatus.OK);
    }

Actual output:

{
  "content": "Hello, World!",
  "_links": {
    "self": {
      "href": "http://localhost:9086/greeting?name=World"
    },
    "ACTIONS": {
      "href": "http://localhost:8080/something",
      "template": "http://localhost:8080/something/v1/books",
      "type": "application/hal+json",
      "method": "GET",
      "describedBy": "http://localhost:8080/something/v1/bookBy"
    }
  }
}

Expected output:

{
  "content": "Hello, World!",
  "_links": {
    "self": {
      "href": "http://localhost:9086/greeting?name=World"
    },
    "ACTIONS": [{
      "href": "http://localhost:8080/something",
      "template": "http://localhost:8080/something/v1/books",
      "type": "application/hal+json",
      "method": "GET",
      "describedBy": "http://localhost:8080/something/v1/describedBy"
    }]
  }
}

I would like to show action as an array. If I add more than one element in ACTIONS attribute then it shows array but if we have only one element then it is showing as an object. I would always like to display that element as an array. Any help would be really appreciable.

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 @EnableHypermediaSupport configuration and the greeting.add(SuperLink) call shown in the issue, then trace how the ACTIONS relation is serialized when it has one versus multiple links. Done means a single ACTIONS link is serialized as an array while preserving the shown link properties and HAL representation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.