小编Igo*_*oso的帖子

如何使用WebClient使用Reactive Spring Rest API

我需要在后端作业(可执行jar)上使用反应式rest API(使用spring webflux构建).

我读过有关Spring WebClient的内容,但我不了解一些观点.

例如:

WebClient webClient = WebClient.create("http://localhost:8080");

Mono<Person> person = webClient.get()
        .uri("/persons/{id}", 42)
        .accept(MediaType.APPLICATION_JSON)
        .exchange()
        .then(response -> response.bodyToMono(Person.class));
Run Code Online (Sandbox Code Playgroud)

在最后一行,有一个"bodyToMono".这是我的问题:

如果被调用的Rest API已经是一个被动服务,我是否需要将响应转换为单声道?有什么地方我错过了吗?

从我的角度来看,我认为可以有一种方法在代码中明确表示我的Rest API是被动的,但可能是我不知道的事情.

rest spring spring-mvc reactive-programming spring-webflux

10
推荐指数
1
解决办法
5025
查看次数

Spring Redis 通过通配符键获取值

我正在使用 Spring Data RedisTemplate(不是存储库)。一切正常

template.opsForValues().get("mykey:1")
Run Code Online (Sandbox Code Playgroud)

但是我有一些更复杂的键,例如“myobject: somesituation :1”和“myobject:anothersituation:2”等等。我需要做类似的事情:

template.opsForValues().get("myobject:somesituation:*")
Run Code Online (Sandbox Code Playgroud)

使用通配符,我想获取“某种情况”中的所有值,无论其 ID 是什么。

使用redis命令行,我没有问题。

观察:我正在使用反应式模板,不知道(相信)这是否可能是问题所在。Obs2:经过研究,我刚刚找到了关于 Spring Repository、获取所有密钥、通过命令行获取等的帖子。但不是关于我的具体问题。

java spring redis spring-data spring-boot

6
推荐指数
1
解决办法
9266
查看次数

使用Spring Webflux返回元素列表

我正在尝试使用Spring Webflux创建一个CRUD的简单示例,但是我陷入了一些概念中。

我知道我可以在控制器请求映射方法中返回Flux。但是有时候我想返回404状态,所以我可以以某种方式处理前端。

我在官方文档中找到了一个使用ServerResponse对象的示例:

        public Mono<ServerResponse> listPeople(ServerRequest request) { 
                Flux<Person> people = repository.allPeople();
                return ServerResponse.ok().contentType(APPLICATION_JSON).body(people, Person.class);
        }
Run Code Online (Sandbox Code Playgroud)

如您所见,即使返回是一个列表(Flux)或person,ServerResponse.ok.body也会创建一个Mono。

所以我的问题是:是这样吗?换句话说,如果我有Flux没关系,Spring是否总是返回Mono的ServerResponse(或其他类似的类)?

对于我的404状态,我可以使用类似

.switchIfEmpty(ServerResponse.notFound().build());
Run Code Online (Sandbox Code Playgroud)

但是我在想更多的流媒体方式。例如,我可以逐个元素地处理对象列表。

mono spring spring-mvc flux spring-webflux

5
推荐指数
1
解决办法
1742
查看次数

spring webflux 支持 javax bean 验证吗?

@NotNull我已经用一些@Valid注释了我的bean,并在@GetMapping. 但这没有用。

我与其他应用程序的唯一区别是我使用@EnableWebMvc而不是@EnableWebFlux.

在控制器中:

    @PostMapping(value = "/something")
    public Mono<ResponseEntity> save(
                                @Valid @RequestBody MyBean mybean) {
        return myService.save(myBean)
                .map(RestResponses::ok)
                .defaultIfEmpty(RestResponses.empty());
    }
Run Code Online (Sandbox Code Playgroud)

在Application.java中:

    @SpringBootApplication
    @EnableWebFlux
    public class Application {

        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的豆类:

    import org.springframework.data.annotation.Id;
    import org.springframework.data.redis.core.RedisHash;
    import org.springframework.data.redis.core.index.Indexed;

    import javax.validation.constraints.NotNull;
    import java.util.Objects;

    @RedisHash("mybean")
    public class MyBean {

        @Id
        private Long id;

        @NotNull
        @Indexed
        private String name;

        //getters, setters...

    }
Run Code Online (Sandbox Code Playgroud)

和 pom.xml:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.M1</version> …
Run Code Online (Sandbox Code Playgroud)

java validation spring spring-mvc spring-webflux

5
推荐指数
1
解决办法
2726
查看次数

Spring Reactive WebClient

我有一个反应式休息api(webflux),也使用spring WebClient类,从其他休息服务请求数据.

简化设计:

@PostMapping(value = "/document")
public Mono<Document> save(@RequestBody Mono<Document> document){

//1st Problem: I do not know how to get the documentoOwner ID 
//that is inside the Document class from the request body without using .block()
    Mono<DocumentOwner> documentOwner = documentOwnerWebClient()
       .get().uri("/document-owner/{id}", document.getDocumentOwner().getId())
       .accept(MediaType.APPLICATION_STREAM_JSON)
       .exchange()
       .flatMap(do -> do.bodyToMono(DocumentOwner.class));

    //2nd Problem: I need to check (validate) if the documentOwner object is "active", for instance
    //documentOwner and document instances below should be the object per se, not the Mono returned from the …
Run Code Online (Sandbox Code Playgroud)

java project-reactor reactive

5
推荐指数
1
解决办法
1070
查看次数

端到端响应式流 RESTful 服务

我已经阅读了这篇文章,我不知道这是否是一个愚蠢的问题。我是反应式编程的新手。

问题很简单:假设我有一个完全反应式的后端,我怎样才能流式传输到浏览器(例如一个大文本),并在每个块来自服务器时立即将它们打印给用户?

可能我遗漏了一些重要的概念点,但我需要知道的是:我可以通过一个 HTTP GET(?或不)请求发送一小部分数据(从服务器到浏览器)吗?关键是:我可以写那些小部分,直到全部数据发送出去吗?

谢谢你的帮助。我之前确实尝试过 google,但我总是得到有关其他概念(如 websockets、长轮询、react.js)的信息,我认为情况并非如此。

编辑:我不是要求特定的 API ou 库。我只是想理解这个概念,例如:“你不能用 HTTP 协议做到这一点,句号!” 或“这与响应式编程无关,您对流的真正含义感到困惑。请参阅'something_else.js'”。

EDIT2:我使用 spring-mvc (spring-boot) 和这个方法做了一个小的休息控制器:

@RequestMapping(value = "/flush", method = RequestMethod.GET)
public void flushTest(HttpServletResponse response) {

    response.setContentType("text/html");
    response.setStatus(SC_OK);

    try (ServletOutputStream outputStream = response.getOutputStream()) {
        for (int i = 0; i < 3; i++) {
            String chunk = "chunk_" + i;
            outputStream.print(chunk);
            outputStream.flush();
            response.flushBuffer();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

}
Run Code Online (Sandbox Code Playgroud)

在浏览器中,即使使用了flushBuffer,也只有整个响应到达。经过一番研究,这可能是一个 TOMCAT 问题,我改变了我的 pom.xml,包括 undertow:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

使用默认配置:

server.undertow.accesslog.enabled=true
server.undertow.accesslog.dir=target/logs …
Run Code Online (Sandbox Code Playgroud)

java reactive-programming rxjs

4
推荐指数
1
解决办法
2838
查看次数