标签: spring-webflux

响应式 Spring 授权服务器

我正在尝试使用 spring-authorization-server,它似乎工作得很好,但正如我所看到的,它依赖于 servlet api,这使得它不能与 spring webflux 和 netty 一起使用。有谁知道如何解决这个问题?或者如果我的假设是错误的,也许可以纠正我。

谢谢

java spring spring-security oauth-2.0 spring-webflux

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

Springboot 3 Webflux + Spring Security CSRF 禁用不起作用

今天我将我的 Webflux REST API 演示应用程序从 springboot 升级2.7.x到了 version 3.0.0。在使用 SpringSecurity 测试 POST 调用时,我收到了403 Forbidden消息An expected CSRF token cannot be found。我仔细检查了我的安全配置,没有发现任何问题。

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
        return http
            .csrf().disable()
            .authorizeExchange()
            .pathMatchers(HttpMethod.GET, "/actuator/**").permitAll()
            .pathMatchers(HttpMethod.POST, "/api/v1/users", "/api/v1/users/**").hasRole(ReactiveConstant.SECURITY_ROLE_ADMIN)     // Only admin can do POST
            .pathMatchers(HttpMethod.GET, "/api/v1/users", "/api/v1/users/**").hasAnyRole(ReactiveConstant.SECURITY_ROLE_USER, ReactiveConstant.SECURITY_ROLE_ADMIN)       // user can only do GET
            .anyExchange().authenticated()
            .and().formLogin()
            .and().httpBasic()
            .and().formLogin().disable()
            .build();
    }
Run Code Online (Sandbox Code Playgroud)

这适用于 SpringBoot 2.7.5 版本。我的build.gradle文件,

plugins {
    id 'org.springframework.boot' version '3.0.0'
    id 'io.spring.dependency-management' version '1.1.0' …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-boot spring-webflux

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

Spring Boot 3.0.4 webflux 具有 415 UNSUPPORTED_MEDIA_TYPE

从 2.7.5 升级到 Spring Boot 3.0.4 webflux 后,我收到“415 UNSUPPORTED_MEDIA_TYPE”。服务正在接受并返回 xml 内容。之前工作得很好。

    @PostMapping(
      value = "/sides/Request",
      consumes = MediaType.APPLICATION_XML_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE)
  public Mono<ResponseModel> getSsi(@RequestBody Mono<RequestModel> requestModelMono) {...
}
Run Code Online (Sandbox Code Playgroud)

聚甲醛:

 <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
 
        </dependency>

    

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
          
            <scope>runtime</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot spring-webflux

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

Spring Web Flux和Reactor中的Map与FlatMap

我一直在使用Spring Boot 2.0.1及其Webflux库来研究示例反应式Web api。我一直在网上查看示例以尝试构建它,但是我在两件事上感到困惑。以下是我有两个问题。

1)如何返回响应实体的流量,当我尝试时出现错误,提示只能返回单个响应实体。下面是我当前的代码。

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieRepository movieRepository;

    @Override
    public Flux<Movie> list(){
        return movieRepository.findAll();
    }
}

@RestController
public class MovieRestController {

    @Autowired
    private MovieService movieService;

    @GetMapping(value = "/movies")
    public Flux<Movie> list() {

        return movieService.list();
    }
}
Run Code Online (Sandbox Code Playgroud)

2)更新对象时,我使用flatMap更新保存在Mongo中的对象,然后使用Map将其转换为响应实体。我的问题是为什么我在这里使用flatMap而不是map?我从在线示例中获取了此代码,但是没有示例说明了flatMap的用法。我想了解为什么在这里使用它。下面是代码。

@Service
public class MovieServiceImpl implements MovieService {

    @Autowired
    private MovieRepository movieRepository;

    @Override
    public Mono<Movie> update(String id, MovieRequest movieRequest) {

       return movieRepository.findById(id).flatMap(existingMovie -> {

           if(movieRequest.getDescription() != null){
               existingMovie.setDescription(movieRequest.getDescription());
           }
           if(movieRequest.getRating() != null){
               existingMovie.setRating(movieRequest.getRating());
           }
           if(movieRequest.getTitle() != …
Run Code Online (Sandbox Code Playgroud)

spring-webflux

0
推荐指数
2
解决办法
4940
查看次数

如何处理基于 spring-webflux 的 REST 服务中的异常

我正在使用 spring-webflux 构建 REST 服务。当找不到请求的路径或发生任何其他内部服务器错误时,我需要使用自定义 json 错误响应进行响应。

我有一个GeneralExceptionHandler实现ErrorWebExceptionHandler.

我有一个豆子GeneralExceptionHandler如下。

@Bean
@Order(-2)
public ErrorWebExceptionHandler errorWebExceptionHandler() {
    return new GeneralExceptionHandler();
}
Run Code Online (Sandbox Code Playgroud)

这样我的句柄方法 ( public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable)) inGeneralExceptionHandler就会在出现诸如未找到请求的路径之类的错误时调用。

而且我还可以使用以下内容发送自定义 http 状态代码。

serverWebExchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);

但我不知道如何发送自定义 JSON 作为响应。

首先,这是处理 Spring webflux 中一般错误的正确方法吗?如果是,我该如何响应自定义 JSON 对象?

exception-handling spring-webflux

0
推荐指数
1
解决办法
2543
查看次数

不存在类型变量 T 的实例,因此 Flux&lt;T&gt; 确认为 Mono&lt;? 扩展 R)

我正在实现 Spring webflux 演示应用程序,并且已经像这样编写了我的演示应用程序

package com.abcplusd.application;

import com.abcplusd.domain.Event;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.Date;
import java.util.stream.Stream;

@SpringBootApplication
public class ReactiveClientApplication {

    @Bean WebClient webClient() {
        return WebClient.create("http://localhost:8080");
    }

    @Bean CommandLineRunner demo(WebClient webClient) {
        return args -> {
            webClient.get()
                .uri("/events")
                .accept(MediaType.TEXT_EVENT_STREAM)
                .exchange()
                .flatMap(clientResponse -> clientResponse.bodyToFlux(Event.class))
                .subscribe(System.out::println);
        };
    }
    public static void main(String[] args) {
        new SpringApplicationBuilder(ReactiveClientApplication.class)
            .properties(Collections.singletonMap("server.port", "8081"))
            .run(args);
    }
}
Run Code Online (Sandbox Code Playgroud)

它显示以下错误

Error:(29, 41) java: incompatible types: no instance(s) …
Run Code Online (Sandbox Code Playgroud)

spring-boot react-native-router-flux spring-webflux

0
推荐指数
1
解决办法
6791
查看次数

subscribeOn(Schedulers.parallel()) 不起作用

我正在学习反应堆核心并关注这个https://www.baeldung.com/reactor-core

ArrayList<Integer> arrList = new ArrayList<Integer>();
System.out.println("Before: " + arrList);
Flux.just(1, 2, 3, 4)
  .log()
  .map(i -> i * 2)
  .subscribeOn(Schedulers.parallel())
  .subscribe(arrList::add);

System.out.println("After: " + arrList);
Run Code Online (Sandbox Code Playgroud)

当我执行上面的代码行时,给出。

 Before: []
 [DEBUG] (main) Using Console logging
 After: []
Run Code Online (Sandbox Code Playgroud)

上面的代码行应该在另一个线程中开始执行,但它根本不起作用。有人可以帮我吗?

reactive-programming reactive-streams spring-webflux

0
推荐指数
2
解决办法
2835
查看次数

Mono.subscriberContext() 返回旧上下文

我有一个带有方法 context() 的类 PublishContext 如下:

public static Mono<Object> context(){

    return Mono.empty().subscriberContext( context -> {
        Context context1 = context.put("key", "hello");
        System.out.println((String) context1.get("key"));
        return context1;
    });
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,上下文对象是 Context0@744 和 context1 是 Context@747 这是可以理解的,因为上下文是不可变的并且总是返回一个新的上下文。

在我的主课中,我有以下代码:

public static void main(String[] args) {

    Mono<Object> objectMono = PublishContext.context();
    objectMono.subscribe();

    Mono<Object> objectMono1 = Mono.subscriberContext().flatMap(context ->       {
        System.out.println((String) context.get("key"));
        return Mono.empty();
    });
    objectMono1.subscribe();
}
Run Code Online (Sandbox Code Playgroud)

在这里,我得到的上下文为 Context0@744,即旧上下文,因此得到“上下文为空”异常。有人可以解释这种行为吗?另外,如何访问我从 context() 方法返回的 context0@747?

java reactive-programming project-reactor spring-webflux

0
推荐指数
1
解决办法
4380
查看次数

Spring Boot将来会支持Spring Webflux应用程序的WAR部署吗?

我知道与Spring Framework相反,Spring Boot 不支持Spring WebFlux应用程序的WAR部署。我的问题很简单:将来会否出现?

我的用例是这样的:我们仍然有很多客户仍然生活在传统的“我们将所有内容都部署在应用程序服务器X”上。因此,尽管我们希望推出独立的JAR,但尚未准备就绪。我们大量使用Spring Boot,并且确实希望继续使用Spring Boot,因此放弃它不是一种选择。

我们正在构建反应性应用程序,并希望为此使用Spring WebFlux,但是我们仍然需要部署到应用程序服务器,因此这不是一种选择。在此期间,我们避免使用Spring WebFlux,而只使用Controllers,它虽然有效,但并不那么优雅。因此,我的问题。

spring-boot spring-webflux

0
推荐指数
1
解决办法
164
查看次数

Spring Webclient:非法参数异常没有足够的变量来扩展'comment_count'

我正在使用spring webclient发出带有{comment_count}的url的Facebook图形api请求

但是,得到这个例外

java.lang.IllegalArgumentException: Not enough variable values available to expand reactive spring
Run Code Online (Sandbox Code Playgroud)

代码段:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import reactor.core.publisher.Mono;

@Component
public class Stackoverflow {

    WebClient client = WebClient.create();

    public Mono<Post> fetchPost(String url) {
        // Url contains "comments{comment_count}"
        return client.get().uri(url).retrieve()
                .bodyToMono(Post.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道resttemplate的解决方案,但是我需要使用spring webclient。

illegalargumentexception facebook-graph-api spring-boot spring-webflux spring-webclient

0
推荐指数
1
解决办法
92
查看次数