在一些 flatMap 链之后,我想最终使用之前的所有结果,假设我有以下代码:
func getObservable1() -> Observable<API1Response> {
...
}
func getObservable2(param: API1Response) -> Observable<API2Response> {
...
}
func getObservable3(param: API2Response) -> Observable<API3Response> {
...
}
getObservable1()
.flatMap { api1Response in
return getObservable2(api1Response)
}
.flatMap { api2Response in
return getObservable3(api2Response)
}
.subscribe(onNext: { api3Response in
// I want to use both of api1Response and api2Response here
})
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我应该将 api 响应保存到范围外变量然后使用后者吗?
就像是:
var api1Response: API1Response?
func getObservable1() -> Observable<API1Response> {
...
self.api1Response = api1Response
...
}
...
getObservable1()
...
.subscribe(onNext: …Run Code Online (Sandbox Code Playgroud) 我对下面的代码片段有一个简单的问题。
library(shiny)
ui <- fluidPage(
fileInput("x", "upload file", accept = c(
"text/csv",
"text/comma-seperated-values, text/plain",
".csv")),
tableOutput("my_csv")
)
server <- function(input, output) {
csv <- reactive({
inFile <- input$x
if (is.null(inFile))
return(NULL)
df<- read.csv2(inFile$datapath, header=T)
return(df)
})
output$my_csv <- renderTable({
validate(need(!is.null(csv()),'no file yet.'))
csv()
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我想要的是像 get() 这样的函数来打印上传的 csv 文件的名称。在下一步中,我想创建一个列表(名为“list”),其中上传的文件作为其第一个对象和文件名。因此,如果上传的文件的名称是“squirrel.csv”并且我调用 list$squirrel.csv 我想查看该表。
Spring文档表示需要手动为WebClient配置http客户端来设置超时:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-client-builder -反应器超时。但由于 WebClient 返回响应式 Mono,因此可以(api-wise)应用.timeout方法。
有同样的效果吗?
而且,当一个人使用.timeout方法时,ReactorTimeoutException是我们所期望的。如果手动完成配置(即可以工作),流中是否会出现相同的错误doOnError(TimeoutException.class, ...)?
我有一个像这样的超级实体类:
@Getter
@Setter
@NoArgsConstructor
public class GenericEntity {
@Id
private Long id;
@JsonIgnore
@CreatedBy
private Long createdBy;
@JsonIgnore
@CreatedDate
private Long createdDate;
@JsonIgnore
@LastModifiedBy
private Long updatedBy;
@JsonIgnore
@LastModifiedDate
private Long updatedDate;
@JsonIgnore
@Version
private Integer version = 0;
}
Run Code Online (Sandbox Code Playgroud)
Role 类从 GenericEntity 扩展,如下所示:
@Getter
@Setter
@NoArgsConstructor
public class Role extends GenericEntity {
private String name;
private String desc;
private Integer sort;
}
Run Code Online (Sandbox Code Playgroud)
之后我有像这样的 RoleRepo 接口:
@Repository
public interface RoleRepo extends ReactiveCrudRepository<Role, Long>;
Run Code Online (Sandbox Code Playgroud)
在 Router 函数中,我有 2 个处理程序方法
private Mono<ServerResponse> …Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring Webflux 开发 REST 服务,并且我想使用 Swagger2 为我的 API 生成文档。我发现 Webflux 仅支持 Swagger2 版本 3.0.0 快照。
这是我的配置:
我的 SwaggerConfiguration bean 看起来像这样
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebFlux;
@Configuration
@EnableSwagger2WebFlux
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.description("My Reactive API")
.title("My Domain object API")
.version("1.0.0")
.build())
.enable(true)
.select()
.apis(RequestHandlerSelectors.basePackage("com.mypackage.service.myobject.controller"))
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
我的 springboot 应用程序定义如下:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.config.EnableWebFlux; …Run Code Online (Sandbox Code Playgroud) 我在我的闪亮应用程序中添加了一个反应计。该仪表用于显示与运动员之前所有时间的最小值和最大值相比的最新跳跃高度得分。
selectInput 设置为Athlete最近的日期(max(jumpdata$Date))。我的代码对于反应式仪表最大值非常有效,但对于最小值不会反应式更新。当我运行应用程序时,最小值显示第一个运动员的输入,然后在我更新并选择不同的输入时保持相同的值(但最大值发生变化)。
我不确定障碍在哪里,因为最大值正在更新。
用户界面
library(shiny)
library(shinydashboard)
library(flexdashboard)
library(dplyr)
jumpdata <- read.csv("SO CMJ Dummy.csv")
jumpdata$Date <- as.Date(jumpdata$Date, "%Y-%m-%d")
shinyUI(
fluidPage(
sidebarPanel(width = 3,
selectInput("Athlete", label = "Athlete",
choices = unique(jumpdata$Athlete))),
mainPanel(
fluidRow(
box(title = "Jump Height", gaugeOutput("Gauge_JH"))
))
))
Run Code Online (Sandbox Code Playgroud)
服务器.r
library(shiny)
library(shinydashboard)
library(flexdashboard)
library(dplyr)
jumpdata <- read.csv("SO CMJ Dummy.csv")
jumpdata$Date <- as.Date(jumpdata$Date, "%Y-%m-%d")
shinyServer(function(input, output){
output$Gauge_JH <- renderGauge({
f <- jumpdata %>%
select(Date, Athlete, JumpHeight_cm) %>%
filter(Athlete == input$Athlete & Date == c(max(jumpdata$Date)))
t <- jumpdata …Run Code Online (Sandbox Code Playgroud) Spring Boot应用:
a@RestController接收以下有效负载:
{
"cartoon": "The Little Mermaid",
"characterNames": ["Ariel", "Prince Eric", "Sebastian", "Flounder"]
}
Run Code Online (Sandbox Code Playgroud)
我需要按以下方式处理它:
转换控制器接收到的数据:将角色名称替换为上一步从“卡通人物”微服务接收到的适当 ID。
{
"cartoon": "The Little Mermaid",
"characterIds": [1, 2, 3, 4]
}
使用转换后的数据向“cartoon-db”微服务发送 HTTP POST 请求。
我遇到的问题:
Reactive Programming我需要使用(非阻塞\异步处理)和Spring WebFlux(Mono| Flux)的范例来实现所有这些步骤Spring Reactive WebClient- 但我对该堆栈的经验为零,试图尽可能多地阅读它,再加上谷歌搜索很多但是仍然有很多未解答的问题,例如:
Q1 . 我已经配置了响应式 webClient,向“卡通人物”微服务发送请求:
public Mono<Integer> getCartoonCharacterIdbyName(String characterName) {
return WebClient.builder().baseUrl("http://cartoon-characters").build()
.get()
.uri("/character/{characterName}", characterName)
.retrieve()
.bodyToMono(Integer.class);
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有一个卡通人物名称列表,对于每个角色我需要调用getCartoonCharacterIdbyName(String name)方法,我不确定串联调用它的正确选项,相信正确的选项:并行执行。
写了如下方法: …
reactive-programming spring-boot reactive spring-webflux spring-webclient
告诉我在 R Shiny 中,有两个操作按钮。我想根据我按下的按钮更新数据。但由于某种原因,它只响应第二个按钮,而不响应第一个按钮。解决办法是什么?
if (interactive()) {
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("action_1", "Get 1"),
actionButton("action_2", "Get 2"),
),
mainPanel(
textOutput("result")
),
)
)
server <- function(input, output) {
data <- eventReactive(input$action_1, 1)
data <- eventReactive(input$action_2, 2)
output$result <- renderText(data())
}
shinyApp(ui, server)
}
Run Code Online (Sandbox Code Playgroud) 我有一个代码块如下
var x = Flux.just("A", "B", "C", "D");
x.flatMap(y -> {
System.out.println(y);
return Mono.just(y);
}).subscribe();
Run Code Online (Sandbox Code Playgroud)
成功打印:
A
B
C
D
Run Code Online (Sandbox Code Playgroud)
除了字符串值之外,有没有办法可以打印索引/记录号?
A - 1
B - 2
C - 3
D - 4
Run Code Online (Sandbox Code Playgroud) 我有一个角度复选框列表,需要验证是否至少选择了一个。
复选框项目是动态的(它们来自服务),因此它不是静态列表。一次可能有 3 个元素,另一次可能有 50 多个元素,等等。
模板是这样的:
<tr *ngFor="let recipient of recipients">
<td class="disable edit">
<div>
<span class = "textbluesmall"><input type="checkbox" [checked]="true" value="{{recipient?.participantId }}" name="participant" />{{recipient?.lastName }} ,{{recipient?.firstName }}</span>
</div>
</td>
<td class="disable edit">
<div>
<span>{{recipient?.email }}</span>
</div>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我正在尝试以这种方式解决,但它不起作用
.TS 文件:
var checkboxs=document.getElementsByName('participant');
var okay=false;
for(var i=0,l=checkboxs.length;i<l;i++)
{
if(checkboxs[i].checked){
okay=true;
break;
}
}
if(okay){
return true;
}
else{
return false;
}
}
How can I do this the easiest way?
Run Code Online (Sandbox Code Playgroud)