小编Ani*_* B.的帖子

自动级联bean-validation递归验证,@ Valid注释

我正在验证Jersey中REST资源端点中的POJO:

public class Resource {
    @POST
    public Response post(@NotNull @Valid final POJO pojo) {
        ...
    }
}

public class POJO {
    @NotNull
    protected final String name;

    @NotNull
    @Valid
    protected final POJOInner inner;

    ...
}

public class POJOInner {
    @Min(0)
    protected final int limit;

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

这似乎工作正常.

但是,@Min(0)仅当字段inner具有@Valid注释时才会验证注释.将@Valid注释添加到不是基元的每个字段是不正确的.

有没有办法告诉bean验证器自动递归继续验证,即使没有@Valid注释?我希望我POJO如下:

public class POJO {
    @NotNull
    protected final String name;

    @NotNull
    protected final POJOInner inner;

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

java jax-rs jersey bean-validation

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

为什么 HATEOAS 在使用 Swagger 2.x 启动期间开始为 spring-boot 版本 >= 2.2.x 创建问题?

我将我的项目从 spring-boot 2.1.9 移到了 2.2.0。

在启动项目时,我面临以下error消息。

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.plugin.core.PluginRegistry<org.springframework.hateoas.client.LinkDiscoverer, org.springframework.http.MediaType>' available: expected single matching bean but found 17: modelBuilderPluginRegistry,modelPropertyBuilderPluginRegistry,typeNameProviderPluginRegistry,syntheticModelProviderPluginRegistry,documentationPluginRegistry,apiListingBuilderPluginRegistry,operationBuilderPluginRegistry,parameterBuilderPluginRegistry,expandedParameterBuilderPluginRegistry,resourceGroupingStrategyRegistry,operationModelsProviderPluginRegistry,defaultsProviderPluginRegistry,pathDecoratorRegistry,apiListingScannerPluginRegistry,relProviderPluginRegistry,linkDiscovererRegistry,entityLinksPluginRegistry


Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'linkDiscoverers' defined in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]: Unsatisfied dependency expressed through method 'linkDiscoverers' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.plugin.core.PluginRegistry<org.springframework.hateoas.client.LinkDiscoverer, org.springframework.http.MediaType>' available: expected single matching bean but found 17: modelBuilderPluginRegistry,modelPropertyBuilderPluginRegistry,typeNameProviderPluginRegistry,syntheticModelProviderPluginRegistry,documentationPluginRegistry,apiListingBuilderPluginRegistry,operationBuilderPluginRegistry,parameterBuilderPluginRegistry,expandedParameterBuilderPluginRegistry,resourceGroupingStrategyRegistry,operationModelsProviderPluginRegistry,defaultsProviderPluginRegistry,pathDecoratorRegistry,apiListingScannerPluginRegistry,relProviderPluginRegistry,linkDiscovererRegistry,entityLinksPluginRegistry


***************************
APPLICATION FAILED TO START
***************************

Description:


Parameter 0 of …
Run Code Online (Sandbox Code Playgroud)

java spring spring-hateoas spring-boot swagger-2.0

33
推荐指数
6
解决办法
3万
查看次数

插件 'org.springframework.boot:spring-boot-maven-plugin:' 未找到

我是 Spring Boot 的新手,我打算将 h2 数据库与 maven 项目连接起来。这是我的 pom.Xml 文件。该文件给出了错误插件 'org.springframework.boot:spring-boot-maven-plugin:' not found

 <?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 https://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>2.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>dev.che</groupId>
    <artifactId>stu</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>stu</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build> …
Run Code Online (Sandbox Code Playgroud)

java spring maven spring-boot

28
推荐指数
6
解决办法
5万
查看次数

Spring Cloud 2020.0 不再处理 bootstrap.yml 配置

在我的Spring Boot 项目中,我定义了 4 个配置文件

  1. 演示
  2. 开发
  3. 测试
  4. 产品

YAML 文件中的属性将在启动期间被 HashiCorp Vault 属性替换。为此,我使用 Spring Cloud Vault 库。在 Spring Boot 2.3.x 中一切正常

当我尝试使用 Spring Cloud Vault 3.0.0-SNAPSHOT 版本将项目升级到 Spring Boot 2.4.0 时,属性没有被替换

引导程序.yml

spring:
  cloud:
    vault:
      authentication: APPROLE
      app-role:
        role-id: ${role-id}
        secret-id: ${secret-id}
        role: pres-read
        app-role-path: approle
      uri: ${vault-server}
      connection-timeout: 5000
      read-timeout: 15000
      kv:
        enabled: true
        backend: secret
        application-name: app/pres

Run Code Online (Sandbox Code Playgroud)

应用程序.yml

spring:
  config:
    activate:
      on-profile: 'demo'
Run Code Online (Sandbox Code Playgroud)

应用程序演示.yml

## Server Properties
server:
  port: 8081

spring:
  datasource:
    username: ${pres.db.username}
    password: …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot spring-cloud

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

java.lang.NoSuchMethodError: 'org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.poi-poi-ooxml-5.2.4

由于安全违规威胁,我已从 升级org.apache.poi-poi-ooxml-5.2.3org.apache.poi-poi-ooxml-5.2.45.2.3

现在,我面临运行时异常java.lang.NoSuchMethodError

例外:

[ERROR] ErrorPageFilter - Forwarding to error page from request [/reports/myapp/myreport] due to exception ['org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.builder()']
java.lang.NoSuchMethodError: 'org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.builder()'
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.newPackage(XSSFWorkbook.java:521) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:231) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:227) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:215) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at myapp.reports.service.impl.MyReportsExcelExporter.<init>(MyReportsExcelExporter.java:37) ~[classes/:0.0.1-SNAPSHOT]
Run Code Online (Sandbox Code Playgroud)

代码:

[ERROR] ErrorPageFilter - Forwarding to error page from request [/reports/myapp/myreport] due to exception ['org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.builder()']
java.lang.NoSuchMethodError: 'org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream$Builder org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream.builder()'
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.newPackage(XSSFWorkbook.java:521) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:231) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:227) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:215) ~[poi-ooxml-5.2.4.jar:5.2.4]
    at myapp.reports.service.impl.MyReportsExcelExporter.<init>(MyReportsExcelExporter.java:37) ~[classes/:0.0.1-SNAPSHOT]
Run Code Online (Sandbox Code Playgroud)

查看版本更改,这似乎是一个小升级,但现在现有代码已停止工作。

可能出了什么问题?

java spring apache-poi spring-boot

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

如何将时间以毫秒为单位转换为 ZonedDateTime

我有以毫秒为单位的时间,我需要将其转换为 ZonedDateTime 对象。

我有以下代码

long m = System.currentTimeMillis();
LocalDateTime d = LocalDateTime.millsToLocalDateTime(m);
Run Code Online (Sandbox Code Playgroud)

线

LocalDateTime d = LocalDateTime.millsToLocalDateTime(m);
Run Code Online (Sandbox Code Playgroud)

给我一个错误,说本地日期时间类型未定义methed millsToLocalDateTime

java java-time

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

@NotNull 注释在 Spring 启动应用程序中不起作用

下面是我的 DTO 课程。

public class AbstractDTO extends BaseDTO {

    private Integer createdBy;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_FORMAT)
    @NotNull(message = "createdDate may not be null")
    private LocalDateTime createdDate;

    private Integer lastModifiedBy;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DATE_FORMAT)
    private LocalDateTime lastModifiedDate;

    private Boolean isActive;

    // getter & setters
}
Run Code Online (Sandbox Code Playgroud)

在这里,我试图将 createdDate 字段注释为 @NotNull 但它不起作用。它允许在请求正文中并且在邮递员中执行服务后没有收到任何错误。

我尝试了以下选项,但没有运气。

1)尝试添加maven依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

2) 尝试将 DTO 类注释为 @Validated

3) 尝试使用 @NotNull 注释 createdDate 字段 @Valid 但仍然没有运气。

请帮我解决这个问题。

java spring spring-mvc spring-boot

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

无法为扩展“应用程序”设置未知属性“mainClass”

我在我的项目中使用 Gradle 6.1 版。

该项目的build.gradle如下:

plugins {
    id 'application'
}

application {
    mainClass = 'com.mytestproject.Main'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

group 'com.mytestproject'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行这个命令时./gradlew assemble,它给了我下面给出的错误:

./gradlew assemble

FAILURE: Build failed with an exception.

* Where:
Build file '/com/myproject/build.gradle' line: 6

* What went wrong:
A problem occurred evaluating root project 'JavaTestProject'.
> Could not set unknown …
Run Code Online (Sandbox Code Playgroud)

java gradle gradlew build.gradle

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

Apache PDFBox 找不到类“Loader”。为什么?

我正在使用pdfbox-app-2.0.18.jarpdfbox-app-2.0.17.jar

这里的示例中,我有以下代码:

try (FileOutputStream fos = new FileOutputStream(signedFile);
     PDDocument doc = Loader.loadPDF(inputFile)) {
    
     // code

}
Run Code Online (Sandbox Code Playgroud)

执行此代码后,我收到以下错误:

org.apache.pdfbox.Loader is not found 
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

java pdfbox

8
推荐指数
3
解决办法
3610
查看次数

Spring:@ControllerAdvice 和 Spring AOP 之间的区别

@ControllerAdvice注释允许我们编写适用于各种控制器的全局代码。

我的问题是:

@ControllerAdvice仅适用于控制器级别,Spring AOP 基于切入点。

除此之外,它们之间以及何时使用@ControllerAdviceSpring AOP还有什么区别?

我试图实现类似示例链接中所示的内容。

链接在这里

java spring-mvc

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