小编Abh*_*rty的帖子

Spring WebClient:解析+流非常大的JSON

这个问题类似于来自常规 WebClient 请求的 Spring 反应式流数据,不同之处在于我没有立即从我的 WebClient 获取 JSON 数组,而是这样的:

这个 JSON 对象可能非常大(~100MB),因此需要处理并流式传输到客户端,而不是解析。这是我似乎能够获得正确语义的唯一方法:

{
   "result-set":{
      "docs":[
         {
            "id":"auhcsasb1005_100000"
         },
         {
            "id":"auhcsasb1005_1000000"
         },
         {
            "id":"auhcsasb1005_1000001"
         },
         {
            "id":"auhcsasb1005_1000002"
         },
         ...
         ...
         {
            "EOF":true
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)
WebClient.create()
  .get()
  .retrieve()
  .bodyToMono(DontKnowWhatClass.class)
  .flatMapMany(resultSet -> Flux.fromIterable(resultSet.getDocs()))
Run Code Online (Sandbox Code Playgroud)

但这意味着我在内存中反序列化 100MB 或更多,然后从中创建通量。我想知道的是:我是否遗漏了一些重要的东西?我可以以某种方式从这样的对象创建 Flux 吗?遗憾的是,我现在有办法影响结果集对象的呈现方式。

spring-webflux

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

在 Oauth2 安全(客户端凭据)资源服务器应用程序中为 WebFluxTest 获取 403 Forbidden Forbidden

我有一个反应式(Spring WebFlux)Web 应用程序,其中很少有受保护资源的 REST API。(Oauth2)。要手动访问它们,我需要获取具有客户端凭据授予类型的授权令牌并在请求中使用该令牌。

现在,我需要编写可以通过 Spring 的 WebTestClient 进行调用来调用 API 的测试。我在尝试访问 API 时收到 403 禁止。编写测试用例时我哪里做错了。

以下是我的安全配置:

@EnableWebFluxSecurity
public class WebSecurityConfiguration {

  @Bean
  SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeExchange()
        .pathMatchers(ACTUATOR_ENDPOINT_PATTERN)
        .permitAll()
        .pathMatchers("/my/api/*")
        .hasAuthority("SCOPE_myApi")
        .anyExchange().authenticated()
        .and()
        .oauth2ResourceServer()
        .jwt();
    http.addFilterAfter(new SomeFilter(), SecurityWebFiltersOrder.AUTHORIZATION);

    return http.build();
  }

  @Bean
  public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
      ReactiveClientRegistrationRepository clientRegistrationRepository,
      ReactiveOAuth2AuthorizedClientService authorizedClientService) {

    ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider =
        ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
            .clientCredentials()
            .build();

    AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager =
        new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(
            clientRegistrationRepository, authorizedClientService);
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

    return authorizedClientManager;
  }

  @Bean
  public WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager) {
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth …
Run Code Online (Sandbox Code Playgroud)

java spring-security-oauth2 spring-security-test spring-webflux

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

如何在 spring-webflux 中使用请求和会话范围(截至最新版本)

我正在通过 spring-webflux(Spring Boot 2.1Spring Framework 5.1) 使用反应式编程开发一个 REST Web 服务。我需要创建具有请求级别范围的组件。建议在 Spring MVC 应用程序中使用 @Scope 注解。但我发现这不适用于 webflux 应用程序。

截至最新版本,webflux 中是否有等效功能?

如果不是,那么在每个传入请求上创建组件的新实例的解决方法是什么?

我试图避免使用 new 运算符。

感谢你的建议。

spring-webflux

3
推荐指数
1
解决办法
2218
查看次数

WebFlux 在后台执行请求?

我的方法应该在执行confirm方法后返回结果或错误消息,然后执行一些后台作业。我写了与上面类似的内容,但我不确定then. 我错了吗?如果是,我如何使用 webflux 在后台运行方法?

    public Mono<Void> someMethod(...){
      return someReactiveApiClient.confirm().onErrorMap(...).then(doSomeBackgroundJob);
    }

Run Code Online (Sandbox Code Playgroud)

java spring spring-webflux

3
推荐指数
1
解决办法
3787
查看次数