HATEOAS on Spring Flux/Mono response
Asked Answered
Z

2

14

I've been using Spring HATEOAS following the guidelines:

https://spring.io/guides/gs/rest-hateoas/#initial

package hello;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
public class GreetingController {

    private static final String TEMPLATE = "Hello, %s!";

    @RequestMapping("/greeting")
    public HttpEntity<Greeting> greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {

        Greeting greeting = new Greeting(String.format(TEMPLATE, name));
        greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());

        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
    }
}

Now I want to use a repository and output a Flux/Mono response:

@RestController
class PersonController {

    private final PersonRepository people;

    public PersonController(PersonRepository people) {
        this.people = people;
    }

    @GetMapping("/people")
    Flux<String> namesByLastname(@RequestParam Mono<String> lastname) {

        Flux<Person> result = repository.findByLastname(lastname);
        return result.map(it -> it.getFullName());
    }
}

How can I use Spring HATEOAS on a Flux/Mono response? Is it possible at all?

Zolly answered 12/8, 2017 at 13:20 Comment(0)
M
4

Update as there is support to use HATEOAS with Spring Web Flux.

public class Person extends ResourceSupport
{
    public Person(Long uid, String name, String age) {
        this.uid = uid;
        this.name = name;
        this.age = age;
    }

    private Long uid;
    private String name;
    private String age;

    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

Using the above Person in Controller as follows

@GetMapping("/all")
    public Flux getAllPerson() {
        Flux<List<Person>> data = Flux.just(persons);
        return data.map(x ->  mapPersonsRes(x));
    }

    private List<Resource<Person>> mapPersonsRes(List<Person> persons) {
    List<Resource<Person>> resources = persons.stream()
            .map(x -> new Resource<>(x,
                    linkTo(methodOn(PersonController.class).getPerson(x.getUid())).withSelfRel(),
                    linkTo(methodOn(PersonController.class).getAllPerson()).withRel("person")))
            .collect(Collectors.toList());
    return resources;
}

Or if you want for one person, you can also use Mono

@GetMapping("/{id}")
public Mono<Resource<Person>> getPerson(@PathVariable("id") Long id){
    Mono<Person> data = Mono.justOrEmpty(persons.stream().filter(x -> x.getUid().equals(id)).findFirst());
    Mono person = data.map(x -> {
        x.add(linkTo(methodOn(PersonController.class).getPerson(id)).withSelfRel());
        return x;
    });
    return person;
}

This is simple use of .map function provided by Flux/Mono. I hope this will be helpful for later viewers.

Mellins answered 4/10, 2019 at 7:58 Comment(1)
I'd image people would like to return Resource with Flux<Person> as a field instead of Flux<List<PersonResource>> which kinda missing the whole point of reactivity.Presidency
G
3

I think this project does not support (yet?) the new reactive support in Spring Framework. Your best bet is to reach out to the maintainers and contribute to the project (creating an issue and explaining what you're trying to achieve is a start!).

Grass answered 30/8, 2017 at 8:46 Comment(2)
Has this issue been addressed?Assyriology
Kind of github.com/spring-projects/spring-hateoas/issues/1762Grison

© 2022 - 2024 — McMap. All rights reserved.