spring 相关问题

Spring Framework是Java平台上应用程序开发的开源框架。其核心是对基于组件的体系结构的丰富支持,目前它拥有20多个高度集成的模块。

ImportJava Config Bean在XML配置Spring 4.0.0.Release

如何在XML配置中导入Java Config Bean? 我尝试创建这样的示例: com.fanjavaid.javaconfig; ... @配置 公共类CDCONFIG { @豆 公共压缩

回答 2 投票 0





“没有足够的权限访问本机PKCE协议”

I已在我的WebApp(域:localhost)中添加了带有LinkedIn功能的登录名;但是,我遇到了403禁止错误“不足以访问本机PKCE协议”(我可以重定向到LinkedIn +允许同意屏幕)。重定向到我的WebApp登录页面时,我会收到错误。

回答 0 投票 0



SpringSecurity OAuth2-将参数添加到授权URL

http://projects.spring.io/spring-security-oauth/

回答 1 投票 0

默认GC用于本地图像Springboot应用程序,以及如何更改为ZGC

我有一个非常简单的Springboot应用程序,我从中构建了一个Graalvm本机图像。 我有一个非常简单的Springboot应用程序,我将其构建一个graalvm的本机图像。 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.4.2</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> [...] <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> </plugin> 本机图像成功地创建了,可以运行,服务流量等,快乐。 但是我想知道,在这种情况下使用了什么默认GC? 从文档中看来https://github.com/spring-projects/spring-boot/wiki/spring-boot-3.4.0-m1-m1-rease-notes-notes-notes#paketo-tiny-tiny-builder-for-build--------- - 图像默认图像是paketobuildpacks/builder-jammy-tiny 该基础图像不会修改GC。话虽如此,默认的GC使用了什么? 最终,我想测试“新” ZGC,如何配置本机图像以考虑ZGC?? 您应该看看这个graalvm文档 https://www.graalvm.org/latest/reference-manual/native-image/optimizations-and-performance/memorymanagement/

回答 1 投票 0

如何创建一个春季库,消费者可以自动导入所有豆?

package com.example.demo.service; @Slf4j @Service @RequiredArgsConstructor public final class MyCustomService { ... } package com.example.demo.config; @Configuration @ComponentScan(basePackages = "com.example.demo") public class MyCustomConfig { }

回答 1 投票 0

KafkaAsync消息生产者:重试不起作用

spring一起使用kafka。 目前,我的重点只是生产者。 在服务器端,我在本地计算机上运行了两个Kafka节点,Kafka版本:3.8.1.

回答 0 投票 0



我想使用Springboot @Valid验证HTTP请求字段,但基于同一HTTP请求的另一个字段。 我有以下代码:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication class FieldValidationApplication { public static void main(String[] args) { SpringApplication.run(FieldValidationApplication.class, args); } } import jakarta.validation.Valid; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController class FieldValidationController { @PostMapping("/validate") String question(@Valid @RequestBody SomeRequest someRequest) { return "please validate the field"; } } record SomeRequest(int score, String fieldPositive, String fieldZeroAndNegative ) { } 验证规则非常简单:请求有效载荷具有场得分。 如果字段得分的值严格为正,那么我需要检查字段点阳性是一个有效的字符串,而且该fieldZeroAndEnderngative为null。 例如: { "score": 1, "fieldPositive": "thisisok" } 但不是: { "score": 1 } { "score": 1, "fieldPositive": "" } { "score": 1, "fieldPositive": "below fieldZeroAndNegative should be null", "fieldZeroAndNegative": "not ok" } 对另一个字段的类似规则(下面的代码)。 这是我尝试的,我创建了自定义注释: record SomeRequest(int score, @ValidateThisFieldOnlyIfScoreIsPositive String fieldPositive, @ValidateThisFieldOnlyIfScoreIsZeroOrNegative String fieldZeroAndNegative ) { } import jakarta.validation.Constraint; import jakarta.validation.Payload; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Constraint(validatedBy = ValidateThisFieldOnlyIfScoreIsPositiveValidator.class) @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface ValidateThisFieldOnlyIfScoreIsPositive { String message() default "Field is invalid"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; class ValidateThisFieldOnlyIfScoreIsPositiveValidator implements ConstraintValidator<ValidateThisFieldOnlyIfScoreIsPositive, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { System.out.println("hello, the value of the field fieldPositive is " + value); System.out.println("However, I cannot get the value of the field score"); if (" SomeRequest score " > 0) { //how to get the value of the field score here? return value != null && !value.isEmpty() && value.length() > 3; } if (" SomeRequest score" <= 0) { return value == null; } ... } } import jakarta.validation.Constraint; import jakarta.validation.Payload; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Constraint(validatedBy = ValidateThisFieldOnlyIfScoreIsZeroOrNegativeValidator.class) @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @interface ValidateThisFieldOnlyIfScoreIsZeroOrNegative { String message() default "Field is invalid"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; class ValidateThisFieldOnlyIfScoreIsZeroOrNegativeValidator implements ConstraintValidator<ValidateThisFieldOnlyIfScoreIsZeroOrNegative, String> { @Override public boolean isValid(String value, ConstraintValidatorContext context) { System.out.println("hello, the value of the field fieldZeroAndNegative is " + value); System.out.println("However, I cannot get the value of the field score"); if (" SomeRequest score " <= 0) { //how to get the value of the field score here? return value != null && !value.isEmpty() && value.length() > 3; } if (" SomeRequest score" > 0) { return value == null; } } } 我不确定每个字段使用一个注释是否是开始的方法。 问题: 如何在验证器中获得相同请求的两个字段(或多个字段)? 要根据同一请求对象中另一个字段的值验证一个字段,您需要在类级而不是字段级别应用验证。这样,验证器可以访问对象的所有字段。 Solution:使用类级约束 创建自定义验证注释 创建可以应用于整个类的自定义注释: import jakarta.validation.Payload; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Constraint(validatedBy = SomeRequestValidator.class) @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface ValidSomeRequest { String message() default "Invalid request data"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } 实现验证器 创建一个可以访问所有字段的验证器: import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; public class SomeRequestValidator implements ConstraintValidator<ValidSomeRequest, SomeRequest> { @Override public boolean isValid(SomeRequest request, ConstraintValidatorContext context) { if (request == null) { return true; // Let @NotNull handle null cases } boolean isValid = true; context.disableDefaultConstraintViolation(); // Prevent default message if (request.score > 0) { // If score is positive, fieldPositive must be non-empty, and fieldZeroAndNegative must be null if (request.fieldPositive == null || request.fieldPositive.isEmpty()) { isValid = false; context.buildConstraintViolationWithTemplate("fieldPositive must not be empty when score is positive") .addPropertyNode("fieldPositive") .addConstraintViolation(); } if (request.fieldZeroAndNegative != null) { isValid = false; context.buildConstraintViolationWithTemplate("fieldZeroAndNegative must be null when score is positive") .addPropertyNode("fieldZeroAndNegative") .addConstraintViolation(); } } else { // If score is zero or negative, fieldZeroAndNegative must be non-empty, and fieldPositive must be null if (request.fieldZeroAndNegative == null || request.fieldZeroAndNegative.isEmpty()) { isValid = false; context.buildConstraintViolationWithTemplate("fieldZeroAndNegative must not be empty when score is zero or negative") .addPropertyNode("fieldZeroAndNegative") .addConstraintViolation(); } if (request.fieldPositive != null) { isValid = false; context.buildConstraintViolationWithTemplate("fieldPositive must be null when score is zero or negative") .addPropertyNode("fieldPositive") .addConstraintViolation(); } } return isValid; } } 将注释对记录进行了 修改SomeRequest使用新的验证注释: @ValidSomeRequest public record SomeRequest(int score, String fieldPositive, String fieldZeroAndNegative) { } 在控制器中进行估算 spring引导将在处理请求时使用注释自动验证sayerquest: import jakarta.validation.Valid; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController class FieldValidationController { @PostMapping("/validate") String validate(@Valid @RequestBody SomeRequest someRequest) { return "Request is valid"; } } 为什么这起作用 验证是在班级级别执行的,因此所有字段均可访问。 验证逻辑在一个地方都检查score和依赖字段。 使用addPropertyNode()将custom错误消息分配给特定字段,从而提高了API响应中的清晰度。 这种方法可确保根据分数对您的请求进行正确验证,而无需为每个字段提供单独的注释。

回答 1 投票 0





如何阻止kafka addriminclient重新连接回试? 我正在尝试为Kafka编写健康检查,该检查将定期运行,检查连接性并通过Prometheus Alerts报告任何问题。我对如何做的想法就是简单地提出请求...

我的问题是单位测试。检查积极的情况很简单并且有效。但是,当我想禁用Embeddedkafka时,它会陷入循环,试图重新连接60秒。我发现甚至很奇怪的是,在重新连接的这60秒内,我的计划任务

回答 0 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.