CompletableFuture:控制http响应状态码(CompletableFuture: control http response status-codes)
我目前正在努力在特定条件下返回http响应状态码。 比方说,taskService.getNewTasks的返回对象为null。 在这种情况下,我想返回状态码404.在某些例外情况下,我想返回一些50x,依此类推。
我的代码到目前为止
@RestController public class TaskController { @Autowired private TaskService taskService; @GetMapping(path = "gettasks") private Future<Tasks> getNewTasks() { return taskService.getNewTasks(); } ... } @Service public class TaskService { @Async public Future<Tasks> getNewTasks() { ... return CompletableFuture.completedFuture(tasks); } }
I'm currently struggeling returning http response status-codes on certain conditions. Let's say, the return objetct of taskService.getNewTasks is null. In this case I want to return status-code 404. On some exception I want to return some 50x, and so on.
My code so far
@RestController public class TaskController { @Autowired private TaskService taskService; @GetMapping(path = "gettasks") private Future<Tasks> getNewTasks() { return taskService.getNewTasks(); } ... } @Service public class TaskService { @Async public Future<Tasks> getNewTasks() { ... return CompletableFuture.completedFuture(tasks); } }
原文:https://stackoverflow.com/questions/48111147
最满意答案
这是使用包
dplyr
的?lag
函数的一个选项(我还使用with
来减少几次输入mydata
并将逻辑测试减少到只有Flag
因为0和1自动被解释为逻辑值):require(dplyr) mydata$trans <- with(mydata, ifelse(Flag, paste(dplyr::lag(AG,1), "to", AG), "")) #mydata # AG Flag trans #1 Team_a 0 #2 Team_b 0 #3 CI 0 #4 Team_a 1 CI to Team_a #5 Team_b 0 #6 Team_b 0 #7 C 0 #8 CI 1 C to CI #9 Team_a 0 #10 Team_b 1 Team_a to Team_b
你可以
dplyr::
但我把它留在那里所以它不会与stats::lag
混淆。Here's one option using the
?lag
function from packagedplyr
(I also usewith
to reduce typingmydata
several times and reduce the logical test to onlyFlag
because 0 and 1 are automatically interpreted as logical values):require(dplyr) mydata$trans <- with(mydata, ifelse(Flag, paste(dplyr::lag(AG,1), "to", AG), "")) #mydata # AG Flag trans #1 Team_a 0 #2 Team_b 0 #3 CI 0 #4 Team_a 1 CI to Team_a #5 Team_b 0 #6 Team_b 0 #7 C 0 #8 CI 1 C to CI #9 Team_a 0 #10 Team_b 1 Team_a to Team_b
You could leave out the
dplyr::
but I leave it there so it's not confused withstats::lag
.
相关问答
更多-
R如果条件满足则跳到下一行,然后检查另一个条件(R for skip to the next row if condition met and then another condition to check)[2023-10-24]
您可以使用rle和apply函数: # re-create your matrix trboot3 <- structure(c(0,-1,-1,-1,-1,-1,-1,-1,0,0,-1,0,0,0,0,0,0,0,0,0,1,1,1, 0,0,0,0,1,1,1,0,0,0,0,0,-1,-1,0, 1,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0, -1,0,0,0),.Dim = c(20L, 3L), .Dimnames = list(NULL, c("V7 ... -
scala> val l = List("I", "'d", "like", "to fix", "this", "sentence", "because", "it", "'s", "broken") l: List[String] = List(I, 'd, like, to fix, this, sentence, because, it, 's, broken) scala> l.reduceRight({(s1,s2) => if (s2.startsWith("'")) s1+s2 else ...
-
从前一行获取值并在R中满足条件时连接(Fetch value from previous row and concatenate if condition is met in R)[2021-12-16]
这是使用包dplyr的?lag函数的一个选项(我还使用with来减少几次输入mydata并将逻辑测试减少到只有Flag因为0和1自动被解释为逻辑值): require(dplyr) mydata$trans <- with(mydata, ifelse(Flag, paste(dplyr::lag(AG,1), "to", AG), "")) #mydata # AG Flag trans #1 Team_a 0 #2 Team_ ... -
Excel:如果条件不符合,则使用先前的值,忽略空白(Excel: use previous value if condition not met, ignoring blanks)[2023-10-08]
为了达到这个目标,我会: 在现有A和B列之间插入一个新列 在单元格B2中,将公式=B1+IF(A2"",VLOOKUP(MAX($B$2:B3),B:C,2,FALSE),"") 复制E3应该会给你以后的结果。 To achieve this, I would: Insert a new column between existing A and B c ... -
使用dplyr包,它很简单: result = left_join(df_a, df_b, by = 'ident') 但是,这将复制一些冗余列。 要清理这些,请使用: result = select(result, id = id.x, ident, var = var.y) Using the dplyr package, it’s straightforward: result = left_join(df_a, df_b, by = 'ident') This will however, co ...
-
另一种方法 df <- data.frame(a=c(0, 1, 2, 2, 3), b=c(1, 3, 2, 4, 4)) df$a[c(df$b[-1], 0) == 2] <- df$a[c(df$b[-1], 0) == 2] + 3 Another approach df <- data.frame(a=c(0, 1, 2, 2, 3), b=c(1, 3, 2, 4, 4)) df$a[c(df$b[-1], 0) == 2] <- df ...
-
您可以使用distinct on : select distinct on (t.idshipment) t.* from t where t.percent_sent >= 75 order by t.idshipment, t.percent_sent asc; You can use distinct on: select distinct on (t.idshipment) t.* from t where t.percent_sent >= 75 order by t.idshipment, t ...
-
另一个使用data.table选项 library(data.table) library(xts) dt <- data.table(df) dt[, Price := na.locf(Price, fromLast = TRUE), by = Meal] Another option using data.table library(data.table) library(xts) dt <- data.table(df) dt[, Price := na.locf(Price, fromLa ...
-
在for循环中存储具有多个条件的变量,直到在R中满足条件(Store variable with multiple condition in for loop until condition is met in R)[2023-11-27]
另一种实现输出的方法: pos = integer(10) pos[df>3 & df<7]<-df[which.max(df>3 & df<7)] cbind(df,pos) df pos [1,] 1 0 [2,] 2 0 [3,] 3 0 [4,] 4 4 [5,] 5 4 [6,] 6 4 [7,] 7 0 [8,] 8 0 [9,] 9 0 [10,] 10 0 关于你的问题 i从1开始,在for循环中你有p ... -
R / tibble - 满足可变条件的子集时间序列?(R / tibble - subset time series up to variable condition met?)[2023-11-20]
您可以执行以下操作: df %>% filter(!cumsum(lag(y, default = 0) > 4)) 结果: # A tibble: 3 x 3 t x y1 2011-03-27 01:30:00 -1 1 2 2011-03-27 01:30:01 -5 2 3 2011-03-27 03:45:00 -3 5 You ...