将 Spring Boot 2.5 升级到 3.0 时,我遇到了 Wiremock 的一些问题,可能是由于迁移到 jakarta 命名空间所致。即使升级到最新的wiremock-jre8
ie 2.35.0
(截至 2022 年 12 月)似乎也没有帮助。我收到此错误:
java.lang.NoClassDefFoundError: javax/servlet/DispatcherType
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at com.github.tomakehurst.wiremock.jetty9.JettyHttpServerFactory.getServerConstructor(JettyHttpServerFactory.java:37)
at com.github.tomakehurst.wiremock.jetty9.JettyHttpServerFactory.<clinit>(JettyHttpServerFactory.java:30)
Run Code Online (Sandbox Code Playgroud) 我写了一个小的自定义maven插件,它运行正常..大部分时间.
使用它时,它被配置为在测试阶段运行,我看到它正在执行,没问题.现在问题出现了,当我使用插件在项目中进行mvn clean install或mvn clean deploy时:它失败并显示一条我无法理解的消息.它显然来自我的插件,因为如果我删除它,那么mvn clean install就可以了.
错误消息很长,它有4个类似于下面的跟踪.
我对它来自何处无能为力......任何想法?
Error injecting: private org.eclipse.aether.spi.log.Logger org.apache.maven.repository.internal.DefaultVersionResolver.logger
[ERROR] while locating org.apache.maven.repository.internal.DefaultVersionResolver
[ERROR] while locating java.lang.Object annotated with *
[ERROR] at org.eclipse.sisu.wire.LocatorWiring
[ERROR] while locating org.eclipse.aether.impl.VersionResolver
[ERROR] for parameter 2 at org.eclipse.aether.internal.impl.DefaultArtifactResolver.<init>(Unknown Source)
[ERROR] while locating org.eclipse.aether.internal.impl.DefaultArtifactResolver
[ERROR] while locating java.lang.Object annotated with *
[ERROR] at org.eclipse.sisu.wire.LocatorWiring
[ERROR] while locating org.eclipse.aether.impl.ArtifactResolver
[ERROR] for parameter 2 at org.apache.maven.repository.internal.DefaultArtifactDescriptorReader.<init>(Unknown Source)
[ERROR] while locating org.apache.maven.repository.internal.DefaultArtifactDescriptorReader
[ERROR] while locating java.lang.Object annotated …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用org.springframework.mail.javamail.JavaMailSenderImpl发送一封非常简单的电子邮件.以下是代码:
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo(request.getCustomerEmail());
mailMessage.setSubject("someSubject");
mailMessage.setFrom("vincent@myDomain.com");
mailSender.send(mailMessage);
Run Code Online (Sandbox Code Playgroud)
这是我得到的例外:
Caused by: org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: No MimeMessage content
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:459)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:307)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么会发生这种情况..
任何的想法 ?
我想为我使用 rabbitMq 的服务运行一些验收测试,但我想忽略所有需要服务间通信 (amqp) 的服务。
然而,问题是 Spring 尝试在启动时连接到(不存在的)rabbit 主机,以便它可以注册其使用者。它对每个注释的方法都@RabbitListener
这样做,如果我的服务中有多个侦听器,这可能会因长时间超时而变得非常烦人。
我怎样才能减少这个超时甚至一起阻止@RabbitListener 连接?
我们的(简化的)Rabbit 配置:
@Configuration
@EnableRabbit
public class RabbitMqConfig {
public RabbitMqConfig(
@Value("${rabbitmq.host}") String rabbitHost,
@Value("${rabbitmq.port}") int rabbitPort,
@Value("${exchange.name}") String exchange) {
this.rabbitHost = rabbitHost;
this.rabbitPort = rabbitPort;
this.exchange= exchange;
}
@Bean
DirectExchange directExchangeBean() {
return new DirectExchange(this.exchange, true, false);
}
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(rabbitHost);
connectionFactory.setPort(rabbitPort);
return connectionFactory;
}
@Bean
public RabbitTemplate rabbitTemplate() {
return new RabbitTemplate(connectionFactory());
}
@Bean
public Queue itemDoneQueue() { …
Run Code Online (Sandbox Code Playgroud) 最新的Jacoco插件(仍然是快照版本,0.7.10-SNAPSHOT),有一个很好的新功能来过滤掉Lombok生成的代码(https://github.com/jacoco/jacoco/wiki/FilteringOptions).我们需要做的就是在存储库的根目录下添加一个lombok.config文件
lombok.addLombokGeneratedAnnotation=true
Run Code Online (Sandbox Code Playgroud)
当我在内部生成Jacoco报告时,我看到了差异,我很高兴.
但是,当我的常规质量工作执行并将结果发布到Sonar时,我会得到不同的(即更糟糕的)结果.
为什么我的本地报告和声纳中没有相同的结果?有没有解决方法?
谢谢
文森特
使用 Spring Boot 2.5,我尝试在构建器级别配置一个 webClient,当它收到 401 时,将删除当前令牌,然后再次尝试调用资源(因此,webclient 意识到不再有令牌,将获取在实际调用资源之前创建一个新资源)。
这是我到目前为止所拥有的:
@Bean
WebClient servletWebClient(ClientRegistrationRepository clientRegistrations,
OAuth2AuthorizedClientRepository authorizedClients) {
//this constructor will configure internally a RemoveAuthorizedClientOAuth2AuthorizationFailureHandler,
// and onAuthorizationFailure will be called on it when we get a 401
var oauth = new ServletOAuth2AuthorizedClientExchangeFilterFunction(clientRegistrations, authorizedClients);
oauth.setDefaultClientRegistrationId("keycloak");
return WebClient.builder()
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.apply(oauth.oauth2Configuration())
.build();
}
Run Code Online (Sandbox Code Playgroud)
我可以看到,当使用这个 webClient 时,RemoveAuthorizedClientOAuth2AuthorizationFailureHandler
出现在图片中removeAuthorizedClient
并被调用。如果我通过此 webClient 进行第二次调用,它会在进行实际调用之前自动获取新令牌。
所以现在,我想配置重试部分,我想一个额外的过滤器可以做到这一点:
ExchangeFilterFunction retryOn401Function() {
return (request, next) -> next.exchange(request)
.flatMap((Function<ClientResponse, Mono<ClientResponse>>) clientResponse -> {
if (clientResponse.statusCode().value() == 401) {
log.warn("got an …
Run Code Online (Sandbox Code Playgroud) 从 Spring Boot 2.6.4 升级到 2.6.6 时,我的测试之一(用 Kotlin 编写)失败了:
@Test
fun shouldLogProperMessageIfNotAbleToHitAPI() {
val configValidator = ConfigValidator(GitHubCrawlerProperties(SourceControlConfig(url = "someIncorrectURL",organizationName="someOrg")),mockRemoteSourceControl)
`when`(mockRemoteSourceControl.validateRemoteConfig("someOrg")).thenThrow(NoReachableRepositories("problem !",mock(Exception::class.java)))
val validationErrors=configValidator.getValidationErrors()
assertThat(validationErrors).hasSize(1);
}
Run Code Online (Sandbox Code Playgroud)
Spring Boot 2.6.4 构建通过。当我在 IDE 中单独运行测试时,它在 Spring Boot 2.6.6 中工作,但在 Maven 构建过程中失败。
默认情况下,堆栈跟踪未显示,但在通过 try/catch 包围调用后,我能够获取它,并且它指向 Logback :
java.lang.NullPointerException: null
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:99)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:62)
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:119)
at ch.qos.logback.classic.Logger.buildLoggingEventAndAppend(Logger.java:419)
at ch.qos.logback.classic.Logger.filterAndLog_0_Or3Plus(Logger.java:383)
at ch.qos.logback.classic.Logger.error(Logger.java:538)
at com.societegenerale.githubcrawler.ConfigValidator.getValidationErrors(ConfigValidator.kt:48)
Run Code Online (Sandbox Code Playgroud)
Logback 版本似乎没有改变,我仍然得到 v 1.2.11 。
查看 Logback 源代码,在 ThrowableProxy 中:
java.lang.NullPointerException: null
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:99)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:89)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:62)
at ch.qos.logback.classic.spi.LoggingEvent.<init>(LoggingEvent.java:119) …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring Boot 2.7.1 和spring-boot-starter-batch
. 该批处理需要 2 个不同的WebClient
API 来调用具有不同身份验证系统的 2 个不同的 API,我通过标准 Spring Boot 属性(spring.security.oauth2.client 等)进行配置。
它运行良好,但我意识到批处理在运行时正在侦听端口 8080,因为我已经导入了spring-boot-starter-web
,这可以WebClient
通过注入ClientRegistrationRepository
. 这不是一个主要问题,但它阻止我并行启动批处理两次,因为端口已被使用......所以我想禁用网络服务器部分。
问题是,当我通过属性、代码或依赖项(通过删除spring-boot-starter-web
)禁用网络服务器时,批处理不再启动,因为ClientRegistrationRepository
不再加载,因为我需要
a bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' that could not be found
Run Code Online (Sandbox Code Playgroud)
这是因为 Spring 有一个条件OAuth2ClientAutoConfiguration
:
@AutoConfiguration(before = SecurityAutoConfiguration.class)
@ConditionalOnClass({ EnableWebSecurity.class, ClientRegistration.class })
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Import({ OAuth2ClientRegistrationRepositoryConfiguration.class, OAuth2WebSecurityConfiguration.class })
public class OAuth2ClientAutoConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
由于应用程序的类型不是SERVLET
, but NONE
,因此不会启用此功能。
我尝试过“强制加载”它:
@ImportAutoConfiguration(OAuth2ClientAutoConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
查看源代码,我发现OAuth2ClientAutoConfiguration
实际上加载了 …
我在 gradle 中有一个名为release的自定义任务,我希望这个任务执行清理和构建任务:
task release
{
//do something
clean
build
}
Run Code Online (Sandbox Code Playgroud)
我知道可以从命令行调用任务,例如
gradle build release
Run Code Online (Sandbox Code Playgroud)
但我想知道是否可以在自定义任务中执行构建任务?
我正在编写一个自定义的Gradle插件.我正在为我想在插件中创建的每个功能编写测试.我正在使用java编写插件.
我在apply
从Plugin接口继承的方法中创建我的任务.
我对项目构建生命周期的afterEvaluate阶段中可用的任务有很多依赖性,但不是很快(至少不是在apply
fase中),我无法控制这些外部任务的定义方式.
所以我使用了定义这些任务的依赖关系
project.afterEvaluate((project) -> {
customTask.dependsOn(project.getTasks().getByName("nameOfTheTask"));
});
Run Code Online (Sandbox Code Playgroud)
在测试代码中,我使用JUnit和gradle测试工具包进行以下设置:
@BeforeClass
public static void initializeProject() {
project = ProjectBuilder.builder().build();
customPlugin = new CustomPlugin();
customPlugin.apply(project);
}
Run Code Online (Sandbox Code Playgroud)
我可以通过检索我的任务来检查测试用例中的常规(即在afterEvaluate块之外)依赖项
project.getTasks().findByName("customTask").getDependsOn()
Run Code Online (Sandbox Code Playgroud)
但是,对于为特定生命周期定义的依赖项,或者更一般地,作为闭包,这是不可能的.
有没有办法测试是否为某个生命周期设置了正确的依赖项?或者有没有办法检索注册这样的代码块?
PS我可以使用投射,反射,阴影,修改等但我真的很想听听这是否可以测试,或者我可能会采取错误的方法.谢谢!
java ×7
spring-boot ×4
gradle ×2
spring ×2
build.gradle ×1
email ×1
jakarta-mail ×1
kotlin ×1
logback ×1
lombok ×1
maven ×1
maven-plugin ×1
mockito ×1
rabbitmq ×1
sonarqube ×1
tdd ×1
wiremock ×1