小编Bri*_*zel的帖子

Spring Boot集成测试抛出错误“ java.lang.IllegalStateException:阻止读取5000 MILLISECONDS超时”

该问题发生在通过Spring Boot 2.0.0.M3使用Spring webflux的项目上。以下是项目的依赖关系,

dependencies {
        compile 'org.springframework.boot:spring-boot-starter-actuator',
                'org.springframework.cloud:spring-cloud-starter-config',
                'org.springframework.cloud:spring-cloud-sleuth-stream',
                'org.springframework.cloud:spring-cloud-starter-sleuth',
                'org.springframework.cloud:spring-cloud-starter-stream-rabbit',
                'org.springframework.boot:spring-boot-starter-data-mongodb-reactive',
                'org.springframework.boot:spring-boot-starter-data-redis-reactive',
                'org.springframework.boot:spring-boot-starter-integration',
                "org.springframework.integration:spring-integration-amqp",
                "org.springframework.integration:spring-integration-mongodb",
                'org.springframework.retry:spring-retry',
                'org.springframework.boot:spring-boot-starter-webflux',
                "org.springframework.boot:spring-boot-devtools",
                'com.fasterxml.jackson.datatype:jackson-datatype-joda',
                'joda-time:joda-time:2.9.9',
                'org.javamoney:moneta:1.0',
                'com.squareup.okhttp3:okhttp:3.8.1',
                "net.logstash.logback:logstash-logback-encoder:4.11",
                'org.apache.commons:commons-lang3:3.5'
        compileOnly 'org.projectlombok:lombok:1.16.18'
        testCompile 'org.springframework.boot:spring-boot-starter-test',
                'io.projectreactor:reactor-test',
                'org.apache.qpid:qpid-broker:6.1.2',
                'de.flapdoodle.embed:de.flapdoodle.embed.mongo'

        integTestCompile sourceSets.main.output
        integTestCompile configurations.testCompile
        integTestCompile sourceSets.test.output
        integTestRuntime configurations.testRuntime
    }
Run Code Online (Sandbox Code Playgroud)

我有一个REST API,代码如下:

 @RestController
    @RequestMapping(value="/internal/note")
    @Slf4j
    public class NoteController {

        @Autowired
        NoteRepository noteRepository;

        @GetMapping("/request/{id}")
        public Mono<Note> getNoteByRequestid(@PathVariable("id") String requestid) {
            logger.debug("Requesting note by request id '{}'.", requestid);
            return noteRepository.findByRequestid(requestid).doOnSuccess(note ->
                    logger.debug("Found note '{}'.", note))
                    .doOnSuccess(response -> logger.debug("Successfully found note …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-webflux

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

Spring Reactor:Mono.zip在空Mono上失败

我正在使用Spring Reactor 3.1.0.M3并且有一个用例,我需要从多个来源合并Mono.我发现如果其中一个Monos是一个空Mono,zip会失败而不会出错.

例:

Mono<String> m1 = Mono.just("A");
Mono<String> m2 = Mono.just("B");
Mono<String> m3 = Mono.empty();

Mono<String> combined = Mono.zip(strings -> {
    StringBuffer sb = new StringBuffer();
    for (Object string : strings) {
        sb.append((String) string);
    }
    return sb.toString();
}, m1, m2, m3);
System.out.println("Combined " + combined.block());
Run Code Online (Sandbox Code Playgroud)

添加m3时,响应中的组合子被跳过为空.当我删除m3时,它都按预期工作,并返回"AB".有没有办法通过检测空Mono来处理这个问题?另外,有没有办法让组合器方法知道对象的类型而不必抛出?

java spring project-reactor

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

如何正确地将 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.0.0 M6-添加休眠拦截器

从Spring Boot 2.0.0 M2升级到2.0.0 M6后,我的Hibernate拦截器实现不再起作用。

我以前的实现:

@Configuration
public class HibernateConfiguration extends HibernateJpaAutoConfiguration {

    private HibernateStatisticsInterceptor hibernateStatisticsInterceptor;

    public HibernateConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers, ObjectProvider<List<SchemaManagementProvider>> providers, HibernateStatisticsInterceptor hibernateStatisticsInterceptor) {
        super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers, providers);
        this.hibernateStatisticsInterceptor = hibernateStatisticsInterceptor;
    }

    @Override
    protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
        vendorProperties.put("hibernate.session_factory.interceptor", hibernateStatisticsInterceptor);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是使用M5或M6时,HibernateJpaAutoConfiguration类已更改,并且不再扩展JpaBaseConfiguration。

我尝试为每个YAML配置文件添加拦截器,但是它不起作用。

我的拦截器:

@Component("hibernateStatisticsInterceptor")
public class HibernateStatisticsInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = 5278832720227796822L;

    private ThreadLocal<Long> queryCount = new ThreadLocal<>();

    public void startCounter() {
        queryCount.set(0l);
    }

    public …
Run Code Online (Sandbox Code Playgroud)

hibernate interceptor spring-boot

5
推荐指数
3
解决办法
1958
查看次数

Spring Boot Reactive Mongo在启动时挂起

我有一个使用启动时挂起的反应性驱动程序的Spring Boot应用Mongo DB程序。

日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.1.RELEASE)

2018-05-20 13:45:47.923 DEBUG 26675 --- [           main] s.b.w.r.c.StandardReactiveWebEnvironment …
Run Code Online (Sandbox Code Playgroud)

spring mongodb spring-data-mongodb spring-boot

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

Spring 5 FluxSink在执行fluxSink.next时不发送数据

我有一个GET方法的例子,它生成一个Randon INT并发送回客户端

@GetMapping
public Flux<String> search() {
    return Flux.create(fluxSink -> {

            Random r = new Random();
            int n;
            for (int i = 0; i < 10; i++) {
                n= r.nextInt(1000);
                System.out.println("Creating:"+n);
                fluxSink.next(String.valueOf(n));

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            fluxSink.complete();


    });
}
Run Code Online (Sandbox Code Playgroud)

卷曲:

curl -v -H"Accept:text/event-stream"-X GET'http :// localhost:8080/stream

使用CURL命令使用此代码,我只能在fluxSink.complete发生时查看客户端中的数字.

现在如果我将我的代码更改为:

@GetMapping
public Flux<String> search() {
    return Flux.create(fluxSink -> {
        Thread t = new Thread(() -> {
            Random r = new Random();
            int n;
            for (int …
Run Code Online (Sandbox Code Playgroud)

java project-reactor reactive spring-webflux

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

Spring Webflux WebClient:它是否支持基于摘要的身份验证?

我刚刚查看了所有 spring 5 文档,但没有发现任何与 WebClient 支持开箱即用的基于摘要的身份验证相关的内容。是否有任何解决方法可以使用 webClient 并仍然调用基于摘要的安全 API?

spring spring-security digest-authentication spring-webflux

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

如何在 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
查看次数

Netty 和 Spring Boot 3 中 getRequestURI 为 null

在 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)

spring thymeleaf spring-boot spring-thymeleaf

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