Search code examples
springspring-mvcspring-hateoasspring-webflux

HATEOAS on Spring Flux/Mono response


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?


Solution

  • 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.