小编Bri*_*zel的帖子

对于Spring boot 2,如何将Java8 Time API序列化为时间戳(EPOCH)?

使用spring boot 2.它已经包含了Jackson Java8 API.但是LocalDatetime的JSON格式是一种人类可读的格式.我希望通过杰克逊成为EPOCH,只是一个真正的时间戳.

我知道如何在Java8 API中将LocatDatetime转换为EPOCH.我的问题是如何配置Spring Boot 2来做到这一点.没有添加客户ObjectMapper可以吗?

time jackson spring-boot

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

如何在 Spring5 中使用 BodyInserters 插入请求正文?

我正在使用 Sping webflux 模块并创建一个 WebClient,请求 uri 和请求正文如下:

// create webclient
WebClient wc3 = WebClient.builder()
                            .baseUrl("http://localhost:8080")
                            .defaultCookie("key", "val")
                            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                            .build();
// set uri
WebClient.RequestBodySpec uri1 = wc3.method(HttpMethod.POST).uri("/getDocs");


//  set a request body
WebClient.RequestBodySpec requestSpec1 = WebClient.create().method(HttpMethod.POST).uri("/getDocs")
                                                .body(BodyInserters.fromPublisher(Mono.just("data")), String.class);
Run Code Online (Sandbox Code Playgroud)

当我设置请求正文时,我收到以下编译错误:

Multiple markers at this line
    - Type mismatch: cannot convert from Mono<String> to P
    - The method fromPublisher(P, Class<T>) in the type BodyInserters is not applicable for the arguments 
     (Mono<String>)
Run Code Online (Sandbox Code Playgroud)

Java 编辑器仅显示“在文件中重命名”作为建议。

我不确定我是否完美地使用了 BodyInserters。请建议。

spring spring-boot

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

更改spring boot 2.0 context-path

我想更改spring boot 2的上下文路径,例如我想在http:// localhost:8080/test /上提供服务

我的意思是它不适合我使用spring-boot-starter-webflux:2.0.0.RELEASE

它只与spring-boot-starter-web :: 2.0.0.RELEASE一起使用

我试过了

server.servlet.context-path=/test
Run Code Online (Sandbox Code Playgroud)

但是我没有发生任何事情仍然在url http:// localhost:8080 /

spring spring-boot spring-webflux

2
推荐指数
3
解决办法
8290
查看次数

Spring Flux缓存项是如何管理的

考虑代码示例:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.time.Duration;

import static com.google.common.collect.ImmutableMap.of;

@Component
public class Scratch {

    @Autowired
    private WebClient webClient;

    public Mono<MyClass> getMyClass(Long id) {
        return webClient.get()
                .uri("{id}", of("id", id))
                .retrieve()
                .bodyToMono(MyClass.class)
                .cache(Duration.ofHours(1));
    }
}
Run Code Online (Sandbox Code Playgroud)

规格说明

将保留无限的历史记录,但应用每个项目的到期超时

什么是项目以及缓存什么?

  1. 整个 http 调用都会被缓存。例如,如果我调用方法.get().uri()id = 1任何后续调用都会被缓存吗?或者
  2. Mono<MyClass>被缓存。例如,任何后续调用都Mono.map将使用缓存的值?

在这两种情况下,什么被视为项目?

java spring project-reactor spring-webflux

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

Spring Boot WebClient.Builder bean在传统servlet多线程应用程序中的用法

我想有一个http客户端从Spring Boot 不响应的应用程序调用其他微服务。由于将不使用RestTemplate,因此我尝试使用WebClient.Builder和WebClient。虽然我不确定线程​​安全性。这里的例子:

@Service
public class MyService{
    @Autowired
    WebClient.Builder webClientBuilder;

    public VenueDTO serviceMethod(){
        //!!! This is not thread safe !!!
        WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();

        VenueDTO venueDTO = webClient.get().uri("/api/venue/{id}", bodDTO.getBusinessOutletId()).
                retrieve().bodyToMono(VenueDTO.class).
                blockOptional(Duration.ofMillis(1000)).
                orElseThrow(() -> new BadRequestException(venueNotFound));
                return VenueDTO;
    }
}
Run Code Online (Sandbox Code Playgroud)

此示例中的serviceMethod()将从几个线程中调用,并且webClientBuilder是单个bean实例。WebClient.Builder类包含状态:baseUrl,这似乎不是线程安全的,因为很少有线程可以同时调用此状态更新。同时,WebClient本身似乎是线程安全的,如在多线程环境中使用Spring WebClient的正确方法中的回答中所述

我是否应该使用https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-webclient.html中提到的WebClient.Builder bean

Spring Boot为您创建并预配置了WebClient.Builder。强烈建议将其注入您的组件中,并使用它来创建WebClient实例。

我看到的解决方法之一是在不将任何状态传递给构建器的情况下创建WebClient,而不是:

WebClient webClient = webClientBuilder.baseUrl("http://localhost:8000").build();
Run Code Online (Sandbox Code Playgroud)

我会做:

WebClient webClient = webClientBuilder.build();
Run Code Online (Sandbox Code Playgroud)

并在uri方法调用中传递带有协议和端口的完整url:

webClient.get().uri("full url here", MyDTO.class)
Run Code Online (Sandbox Code Playgroud)

在我的情况下使用它的正确方法是什么?

spring spring-boot spring-webflux

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

SpringBoot 3 与 Spring Cloud Vault 3.1.1 运行时错误 java.lang.NoClassDefFoundError: org/apache/hc/client5/http/classic/HttpClient

当 spring.config.import:Vault:// set 时,Spring-boot 3 中的应用程序无法启动。\n出现以下错误:

\n
16:54:21.649 [restartedMain] ERROR org.springframework.boot.SpringApplication - Application run failed\njava.lang.NoClassDefFoundError: org/apache/hc/client5/http/classic/HttpClient\n
Run Code Online (Sandbox Code Playgroud)\n

描述:

\n
    \n
  • 春季启动3
  • \n
  • JDK 19
  • \n
\n

环境状态:

\n
JAVA_HOME: /Users/USER/Library/jdk-19.0.1.jdk/Contents/Home\n
Run Code Online (Sandbox Code Playgroud)\n

Java版本:

\n
\xe2\x9d\xaf java --version\nopenjdk 19.0.1 2022-10-\nOpenJDK Runtime Environment (build 19.0.1+10\nOpenJDK 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)\n
Run Code Online (Sandbox Code Playgroud)\n

应用程序.yml:

\n
spring:\n  config:\n    import: vault://\n\nspring.cloud.vault:\n  enabled: true\n  application-name: APP\n  host: ${VAULT_HOST}\n  port: 8200\n  scheme: https\n  namespace: admin\n  fail-fast: true\n  config:\n    lifecycle:\n      enabled: true\n      min-renewal: 10s\n      expiry-threshold: 1m\n  authentication: …
Run Code Online (Sandbox Code Playgroud)

java spring-boot spring-cloud-vault-config spring-webflux

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

Spring Boot 3 和 Hibernate 6 迁移错误:不支持从 datetime2 到 DATETIMEOFFSET 的转换

我将 Spring Boot 项目升级到 Spring Boot 3.0.0 和 Hibernate 6.x。应用程序启动时没有任何错误,当我访问任何具有Instant日期类型的表信息时,出现以下错误

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from datetime2 to DATETIMEOFFSET is unsupported.

Run Code Online (Sandbox Code Playgroud)

我正在使用 SQL Server 2016 和 Spring Data 3.0.0

java hibernate spring-boot hibernate-6.x

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

Spring Boot 3 和 OpenApi 3 (1.6.13) 不起作用。白标错误页面

我正在尝试使用 Spring Boot 3、Java 17、Open Api 3 查看 Swagger 页面。

我一定做错了什么,但是什么?是不是有什么不兼容的地方?

在 http://localhost:8080/v3/api-docs/ 或 http://localhost:8080/swagger-ui 我得到Whitelabel错误页面。

我正在从 Spring Boot 2.5 迁移,但为了确保我没有忘记删除导致此错误的某些内容,我从此处复制了一个教程https://www.techgeeknext.com/spring-boot/spring-boot- swagger3-example(我在这篇文章的末尾写了它)。

虽然运行了,但控制台中出现错误:

ERROR Class 'org.springframework.core.io.support.PathMatchingResourcePatternResolver' could not be processed by org.zeroturnaround.javarebel.integration.spring.core.cbp.PathMatchingResourcePatternResolverCBP@jdk.internal.loader.ClassLoaders$AppClassLoader@7a46a697: org.zeroturnaround.bundled.javassist.NotFoundException: retrieveMatchingFiles(..) is not found in org.springframework.core.io.support.PathMatchingResourcePatternResolver
    at org.zeroturnaround.bundled.javassist.CtClassType.getDeclaredMethod(SourceFile:1356)
    at org.zeroturnaround.javarebel.integration.spring.core.cbp.PathMatchingResourcePatternResolverCBP.registerScannedDirs(PathMatchingResourcePatternResolverCBP.java:255)
    at org.zeroturnaround.javarebel.integration.spring.core.cbp.PathMatchingResourcePatternResolverCBP.process(PathMatchingResourcePatternResolverCBP.java:41)
    at org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor.process(SourceFile:137)
    at org.zeroturnaround.javarebel.integration.support.CacheAwareJavassistClassBytecodeProcessor.process(SourceFile:34)
    at org.zeroturnaround.javarebel.integration.support.JavassistClassBytecodeProcessor.process(SourceFile:83)
    at com.zeroturnaround.javarebel.yg.a(SourceFile:413)
    at com.zeroturnaround.javarebel.yg.a(SourceFile:340)
    at com.zeroturnaround.javarebel.SDKIntegrationImpl.runBytecodeProcessors(SourceFile:44)
    at com.zeroturnaround.javarebel.vk.transform(SourceFile:140)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:43009)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    at org.springframework.core.io.support.ResourceArrayPropertyEditor.<init>(ResourceArrayPropertyEditor.java:77)
    at …
Run Code Online (Sandbox Code Playgroud)

spring-boot openapi springdoc java-17

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

为什么@AutoConfiguration使用proxyBeanMethods = false,@AutoConfigureBefore,@AutoConfigureAfter

Spring Boot 自动配置最近在 2.7 版本中进行了更改,并且大多数设置在 3.0 版本中已弃用(您可以在此处找到详细信息)。此外,他们还为自动配置类引入了新的注释,即@AutoConfiguration. 我无法理解下面所述的注释的默认设置:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore
@AutoConfigureAfter
public @interface AutoConfiguration {
}
Run Code Online (Sandbox Code Playgroud)

为什么他们强制用户继承proxyBeanMethods = false,@AutoConfigureBefore@AutoConfigureAfter

spring spring-boot spring-boot-configuration spring-autoconfiguration

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

Spring 5 Reactive - 没有调用WebExceptionHandler

我已经尝试了所有3个解决方案,建议用什么方法来处理spring-webflux中的错误,但是WebExceptionHandler没有被调用.我在用Spring Boot 2.0.0.M7.Github回复这里

@Configuration
class RoutesConfiguration {

  @Autowired
  private lateinit var testService: TestService

  @Autowired
  private lateinit var globalErrorHandler: GlobalErrorHandler

  @Bean
  fun routerFunction():

    RouterFunction<ServerResponse> = router {
    ("/test").nest {

      GET("/") {
        ServerResponse.ok().body(testService.test())
      }
    }
  }


} 


@Component
class GlobalErrorHandler() : WebExceptionHandler {

  companion object {
    private val log = LoggerFactory.getLogger(GlobalErrorHandler::class.java)
  }

  override fun handle(exchange: ServerWebExchange?, ex: Throwable?): Mono<Void> {

    log.info("inside handle")

    /* Handle different exceptions here */
    when(ex!!) {
      is ClientException -> exchange!!.response.statusCode = …
Run Code Online (Sandbox Code Playgroud)

exception-handling reactive-programming spring-boot spring-webflux

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