我正在学习 Spring WebFlux。
我的实体是这样的:
@Table("users")
public class User {
@Id
private Integer id;
private String name;
private int age;
private double salary;
}
Run Code Online (Sandbox Code Playgroud)
我有一个存储库(R2 使用 H2 数据库),如下所示:
public interface UserRepository extends ReactiveCrudRepository<User,Integer> {
}
Run Code Online (Sandbox Code Playgroud)
我的控制器是:
@Autowired
private UserRepository userRepository;
private static List<User> userList = new ArrayList<>();
@PostConstruct
public void initializeStockObjects() {
User stock1 = new User(11, "aaaa", 123, 123);
User stock2 = new User(12, "bbb", 123, 123);
User stock3 = new User(13, "ccc", 123, 123);
userList.add(stock1);
userList.add(stock2);
userList.add(stock3);
}
@RequestMapping(value …Run Code Online (Sandbox Code Playgroud) 我有一个数组和一个如下所示的流。
String[] names = {"Alex", "Anna", "Abhi", "Some", "Broad", "Dustin",
"amanda", "Hanna", "Steve", "Sydney"};
Stream.of(names)
.sorted()
.map(String::toLowerCase)
.filter(x -> x.startsWith("a"))
.forEachOrdered(System.out::println);
//I have also tried - .forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
实际输出:
abhi
alex
anna
amanda
Run Code Online (Sandbox Code Playgroud)
预期输出:
abhi
alex
amanda
anna
Run Code Online (Sandbox Code Playgroud)
我在这里缺少什么?
我有一个String:
String thestra = "/aaa/bbb/ccc/ddd/eee";
Run Code Online (Sandbox Code Playgroud)
在我的情况下,每次刺痛,至少都会出现两次斜线。
我得到类似下面的内容,它是字符串中/aaa/字符的“前两次出现”之间的子字符串。/
System.out.println("/" + thestra.split("\\/")[1] + "/");
Run Code Online (Sandbox Code Playgroud)
它解决了我的目的,但我想知道是否还有其他优雅且干净的替代方案?
请注意,我需要在 周围使用两个斜杠(前导和尾随)aaa。IE/aaa/
在 Thymeleaf < 3.1 中,我使用下面的表达式来获取请求 URI。
th:classappend="${#arrays.contains(urls, #httpServletRequest.getRequestURI()) ? 'active' : ''}"
Run Code Online (Sandbox Code Playgroud)
它一直有效,直到最近我升级到了 Spring Boot 3.0,它拉动了 Thymeleaf 3.1。我收到此异常:
[THYMELEAF][parallel-2] Exception processing template "index": Exception evaluating SpringEL expression: "#arrays.contains(urls, #servletServerHttpRequest.getRequestURI()) ? 'active' : ''" (template: "fragments/header" - line 185, col 6)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getRequestURI() on null context object
Run Code Online (Sandbox Code Playgroud)
既然我在 Spring Boot 3.0 中使用 Netty 而不是 Tomcat,那么现在有什么替代方案吗?我无法从这里弄清楚这一点。
作为解决方法,现在为了解决这个问题,我正在使用:
@GetMapping ("/")
String homePage(Model model) {
model.addAttribute("pagename", "home");
return "index";
}
Run Code Online (Sandbox Code Playgroud)
和
th:classappend="${pagename == …Run Code Online (Sandbox Code Playgroud) 我有一个简单的类,如下所示:
public class ListOfMapsExample {
public static void main(String[] args) {
Map<String, String> map1 = Map.of("aa1k", "aa1v", "aa2k", "aa2v", "aa3k", "aa3v");
Map<String, String> map2 = Map.of("bb1k", "bb1v", "bb2k", "bb2v", "bb3k", "bb3v");
Map<String, String> map3 = Map.of("cc1k", "cc1v", "cc2k", "cc2v", "cc3k", "cc3v");
List<Map<String, String>> maps = Arrays.asList(map1, map2, map3);
List<Map<String, String>> listOfMaps1 = maps.stream()
.map(m -> m.entrySet().stream()
.filter(map -> map.getKey().equals("aa1k") || map.getKey().equals("bb2k"))
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue())))
.collect(Collectors.toList());
System.out.println("listOfMaps1 :: " + listOfMaps1);
}
}
Run Code Online (Sandbox Code Playgroud)
预期输出:
listOfMaps1 :: [{aa1k=aa1v}, {bb2k=bb2v}] …
在我的 bash 脚本中,我有这个:
myapphome=/home/username/Documents/myapp
cat << 'EOT' > "$myapphome"/some.properties
dir.root="$myapphome"/app_data
EOT
Run Code Online (Sandbox Code Playgroud)
预期在 some.properties 中:
dir.root=/home/username/Documents/myapp/app_data
Run Code Online (Sandbox Code Playgroud)
但实际上是这样的:
dir.root="$myapphome"/app_data
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?我的意思是我想在我的 some.properties 文件中扩展 $myapphome 。
我试图在折叠时更改 Bootstrap 5 的手风琴的“手风琴按钮”颜色。
我在下面试过。它会更改背景,但不会更改按钮的图标颜色。
.accordion-button.collapsed {
color: white;
background: blue;
}
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能更改 Bootstrap 5 手风琴的向下(折叠)图标?
我有一个如下所示的 Mono:
private void getNodeDetail() {
Mono<String> mono = webClient.get()
.uri("/alfresco/api/-default-/public/alfresco/versions/1/nodes/f37b52a8-de40-414b-b64d-a958137e89e2")
.retrieve().bodyToMono(String.class);
System.out.println(mono.subscribe());
System.out.println(mono.block());
}
Run Code Online (Sandbox Code Playgroud)
问题:第一个 sysout 向我展示了reactor.core.publisher.LambdaSubscriber@77114efe使用 block() 时的情况,它向我展示了我需要的内容(json 字符串)。但我想使用 Aysnc 方法。那么,如上所述,这是否意味着我的目标系统(在本例中为 Alfresco)不支持异步调用?subscribe()如果不是这种情况,如何使用,就像 一样以字符串格式在控制台上打印响应block()?
我有一个字符串( NOTPath或File-path ),如下所示:
String str1 = "/first/second/third/fourth/fifth";
String str2 = "/foo/bar/blah";
Run Code Online (Sandbox Code Playgroud)
给出:字符串始终以前导斜杠开头,并且保证至少有两个斜杠(也可以更多,如上所述)以及后面的一些子字符串。
现在我试图捕获整个子字符串"after-first-two-slashes"。这样我的输出就变成:
Output:
str1_becomes = /second/third/fourth/fifth
str2_becomes = /bar/blah
Run Code Online (Sandbox Code Playgroud)
请注意,我也想保留前导斜杠。我应该采取什么有效的方法来实现它?需要一些指示/线索,因为在这里感觉迷失了。最好是基于java 1.8+。
java ×4
spring ×3
java-8 ×2
java-stream ×2
spring-boot ×2
alfresco ×1
bash ×1
bootstrap-5 ×1
css ×1
dictionary ×1
h2 ×1
heredoc ×1
r2dbc ×1
sorting ×1
string ×1
substring ×1
thymeleaf ×1
variables ×1