小编Bri*_*zel的帖子

如何正确地将 Flux 包裹在 Mono 对象中

我有一个网络服务,它返回学生和注册课程的详细信息。

{
  "name": "student-name",
  "classes": [
    {
      "className": "reactor-101",
      "day": "Tuesday"
    },
    {
      "className": "reactor-102",
      "day": "Friday"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

该类的 DTO 如下:

public class Student {
        private String name;
        private Flux<StudentClass> classes;
        @Data
        @AllArgsConstructor
        @JsonInclude(JsonInclude.Include.NON_DEFAULT)
        public static class StudentClass {
            private String className;
            private String day;
        }
    }
Run Code Online (Sandbox Code Playgroud)

获取学生的主要 REST 控制器逻辑如下:

Flux<StudentClass> studentClassFlux = studentClassRepository.getStudentClass(studentName);

return Mono.just(new Student(studentName, studentClassFlux));
Run Code Online (Sandbox Code Playgroud)

问题是,在进行 REST 调用后,我得到以下输出:

{
  "name": "student-name",
  "classes": {
    "prefetch": 32,
    "scanAvailable": true
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以通过阻止通量请求以完成所需的输出,然后将输出转换为列表来实现所需的输出。

List<StudentClass> studentClassList = studentClassRepository.getStudentClass(studentName)..toStream().collect(Collectors.toList());
return …
Run Code Online (Sandbox Code Playgroud)

project-reactor reactive-streams spring-webflux

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

默认情况下未启用 Spring Boot Actuator 路径?

在将我的 Spring Boot 应用程序更新到最新的构建快照时,我发现默认情况下没有启用任何执行器端点。如果我指定在 中启用application.properties它们,它们就会出现。

1) 这种行为是有意为之吗?我试图寻找一个问题来解释它,但找不到一个。有人可以将我链接到问题/文档吗?

2)有没有办法启用所有执行器端点?我经常发现自己在开发过程中使用它们,而不想在我的属性文件中维护它们的列表。

spring-boot spring-boot-actuator

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

在Spring Boot 2应用程序的JUnit 5测试中模拟自动连接依赖项

考虑以下测试类:

public class SomeClass {

    @Autowired
    private SomeDependency someDependency;

    public int inc(int i) {
        someDependency.doSomething();
        return i + 1;
    }

}
Run Code Online (Sandbox Code Playgroud)

如何someDependency在JUnit 5(5.0.1)测试中为Spring Boot 2(2.0.0.M2)应用程序模拟(最好使用Mockito)?当我尝试调用SomeClass#inc(int)它时会产生一个NullPointerException因为没有注入自动连接的依赖项.

junit spring spring-boot junit5

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

Spring Boot 2.0.0.M6:通过一个请求显示所有指标

当我要求使用Spring Boot 2.0.0.M6和新的执行器指标端点时

GET /application/metrics

仅显示指标名称

{
  "names" : [ "data.source.active.connections", "jvm.buffer.memory.used", "jvm.memory.used", "jvm.buffer.count", "logback.events", "process.uptime", "jvm.memory.committed", "data.source.max.connections", "http.server.requests", "system.load.average.1m", "jvm.buffer.total.capacity", "jvm.memory.max", "process.start.time", "cpu", "data.source.min.connections" ]
}
Run Code Online (Sandbox Code Playgroud)

显然,我可以使用来访问特定指标 GET /application/metrics/jvm.memory.used

但是,有一种方法可以通过一个请求查看所有指标吗?

spring spring-boot spring-boot-actuator

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

不可解析的导入POM未找到

我在父pom中尝试像模块一样的弹簧启动,现在我得到了错误

[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] Non-resolvable import POM: Failure to find org.springframework.boot:spring-boot-dependencies:pom:2.0.0.M6 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced @ line 30, column 19
 @ 
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project mydomain.project:main-project:1.0-SNAPSHOT (D:\gitProjects\main-server\sources\pom.xml) has 1 error
[ERROR]     Non-resolvable import POM: Failure to find org.springframework.boot:spring-boot-dependencies:pom:2.0.0.M6 in https://repo.maven.apache.org/maven2 …
Run Code Online (Sandbox Code Playgroud)

java dependency-management maven spring-boot

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

如何在Spring Boot 2(带有WebFlux)中为HTTP和HTTPS配置两个端口?

谁能告诉我使用Spring Boot 2和WebFlux时如何配置2个端口(用于HTTP和HTTPS)?任何提示表示赞赏!

spring-boot spring-webflux

5
推荐指数
2
解决办法
1955
查看次数

使用 spring-webflux 忽略网络

在 spring-mvc 中可以扩展 from WebSecurityConfigurerAdapter,覆盖configure(WebSecurity web)并做一些这样的思考:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(AUTH_WHITE_LIST);
}
Run Code Online (Sandbox Code Playgroud)

这种方法的主要好处是 spring-security 甚至不会尝试解码传递的令牌。是否可以使用 webflux 做几乎相同的事情?

我知道我可以这样做:

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) throws Exception {
    http.csrf().disable()
            .authorizeExchange().pathMatchers(AUTH_WHITE_LIST).permitAll()
            .anyExchange().authenticated();
    return http.build();
}
Run Code Online (Sandbox Code Playgroud)

但是这样,据我所知,spring-security 将首先尝试解析提供的令牌。

java spring-security spring-webflux

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

如何在 webflux 中调试?

我使用带有 webflux 的 Spring Boot 2.1.1.RELEASE。

依赖关系如下:

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

这是控制器,我使用 Hooks.onOperatorDebug(); 正如 reactor 的文档所说,它可以打开调试模式。

@RestController
public class TestController {
    @GetMapping("/test")
    public Mono test(String a) {
        Hooks.onOperatorDebug();
        return Mono.just("test1")
                .map(t -> t + "test2")
                .zipWith(Mono.error(() -> new IllegalArgumentException("error")));
    }

    @PostMapping("/test")
    public Mono post(@RequestBody Req req) {
        return Mono.just(req);
    }
}

class Req {
    private String a;
    private String b;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getB() …
Run Code Online (Sandbox Code Playgroud)

spring-boot project-reactor spring-webflux

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

Spring Boot 3 安全 requestMatchers.permitAll 不起作用

下面是SecurityFilterChain根据新的 Spring Security 6 / Spring boot 3 文档创建的 bean。但是,requestMatchers -> AntPathRequestMatcher -> permitAll不起作用。每个请求都达到了OncePerRequestFilter。请告诉我这是预期的结果还是出现问题。

代码:

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableMethodSecurity
public class WebSecurityConfig {
   List<String> publicApis = List.of("/generate/**", "validated/**");
   
   @Bean
   public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .cors()
                .configurationSource(corsConfigurationSource())
                .and()
                .csrf()
                .disable()
                .formLogin()
                .disable()
                .httpBasic()
                .disable()
                .exceptionHandling()
                .authenticationEntryPoint(restAuthenticationEntryPoint())
                .and()
                .authorizeHttpRequests(
                        r -> r.requestMatchers(
                                        publicApis().stream()
                                                .map(AntPathRequestMatcher::new)
                                                .toArray(RequestMatcher[]::new)
                                )
                                .permitAll()
                                .anyRequest()
                                .authenticated()
                )
                .sessionManagement(s -> s.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .addFilterBefore(tokenAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);

        return http.build();
    }

    @Bean
    public WebSecurityCustomizer webSecurityCustomizer() { …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

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

Spring Boot 3 Native 与 lombok 不可变(@Value)

我一直在尝试使用 spring boot 3、graalvm、lombok 和 jpa 创建一个示例项目。当我使用 main 方法执行测试时,一切看起来都很完美。但是当我运行本机二进制文件时遇到问题。

首先我创建二进制文件:

mvn -Pnative native:build
Run Code Online (Sandbox Code Playgroud)

然后我运行该项目:

./target/demo
Run Code Online (Sandbox Code Playgroud)

项目开始正常,但是当我尝试使用curl 创建新记录时:

curl --location --request POST 'localhost:8080' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "test",
    "surname": "surname"
}'
Run Code Online (Sandbox Code Playgroud)

我在控制台中看到此错误:

2022-12-21T01:51:48.055+01:00  WARN 105751 --- [nio-8080-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class com.example.demo.dto.ExampleDto]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Builder class `com.example.demo.dto.ExampleDto$ExampleDtoBuilder` does not have build method (name: 'build')
2022-12-21T01:51:48.055+01:00  WARN 105751 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content-Type 'application/json;charset=UTF-8' is not supported]

Run Code Online (Sandbox Code Playgroud)

我的 DTO …

native jackson lombok spring-boot graalvm

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