【问题标题】:HATEOAS on Spring Flux/Mono responseHATEOAS 关于 Spring Flux/Mono 响应
【发布时间】:2018-01-20 21:20:48
【问题描述】:

我一直按照指南使用 Spring HATEOAS:

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);
    }
}

现在我想使用存储库并输出 Flux/Mono 响应:

@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());
    }
}

如何在 Flux/Mono 响应中使用 Spring HATEOAS?有可能吗?

【问题讨论】:

    标签: spring spring-mvc spring-hateoas spring-webflux


    【解决方案1】:

    更新,因为支持将 HATEOAS 与 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;
        }
    }
    

    在Controller中使用上面的Person如下

    @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;
    }
    

    或者如果你想要一个人,你也可以使用 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;
    }
    

    这是对 Flux/Mono 提供的.map 函数的简单使用。 我希望这对以后的观众有所帮助。

    【讨论】:

    • 我想人们想用Flux&lt;Person&gt; 作为字段返回Resource,而不是Flux&lt;List&lt;PersonResource&gt;&gt;,这有点缺少反应性。
    【解决方案2】:

    我认为这个项目不支持(还?)Spring 框架中的新反应式支持。你最好的选择是reach out to the maintainers and contribute to the project(创建一个问题并解释你想要实现的目标是一个开始!)。

    【讨论】:

    • 这个问题解决了吗?
    猜你喜欢
    • 2020-03-17
    • 2021-09-30
    • 2018-06-07
    • 2018-12-19
    • 2018-12-12
    • 2019-05-18
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多