Jersey @Valid 注释问题:InjectingConstraintValidatorFactory 的 NullPointerException

Jersey @Valid Annotation Issue: NullPointerException at InjectingConstraintValidatorFactory

提问人:Narayan Ghimire 提问时间:9/12/2023 最后编辑:zforgoNarayan Ghimire 更新时间:9/12/2023 访问量:48

问:

我正在开发一个 Java Jersey 应用程序,我需要使用 @Valid 注释验证 JSON 请求正文。但是,我遇到了一个我似乎无法解决的 NullPointerException。以下是我的设置和问题的详细信息:

JSON 请求正文(示例):这是我需要验证的JSON请求正文:

{"firstName": "f"}

泽西岛应用程序配置 (JerseyConfig.java):

package com.api.customer;

import com.api.customer.Exception.ExceptionHandler;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;

import javax.ws.rs.ApplicationPath;

@ApplicationPath("/api/v1.0")
public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        packages("com.api.customer");
        register(ExceptionHandler.class);
        property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
    }
}

控制器(CustomerController.java):

package com.api.customer.Http.Controller;

import com.api.customer.Entity.Welcome;
import com.api.customer.Http.Request.UserRequest;
import com.api.customer.Http.Responses.WelcomeResponse;
import com.api.customer.Services.CustomerService;

import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.validation.Valid;

@Path("/customer")
public class CustomerController {
    private final WelcomeResponse welcomeResponse;
    private final CustomerService customerService;

    @Inject
    public CustomerController(
            WelcomeResponse welcomeResponse,
            CustomerService customerService
    ) {
        this.welcomeResponse = welcomeResponse;
        this.customerService = customerService;
    }

    @POST
    @Produces("application/json")
    @Consumes(MediaType.APPLICATION_JSON)
    public Welcome create(@Valid UserRequest userRequest) {
        String firstName = userRequest.getFirstName();

        this.customerService.createCustomer();

        return welcomeResponse.getWelcomeResponse();
    }
}

pom.xml:

<?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>

    <groupId>com.api.customer</groupId>
    <artifactId>CustomerApi</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>CustomerApi</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.9.2</junit.version>
        <jersey.version>2.34</jersey.version>
        <junit.version>5.9.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se-core</artifactId>
            <version>3.1.9.Final</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.42.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-bean-validation</artifactId>
            <version>${jersey.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

问题描述:我不断收到 500 内部服务器错误,并显示以下堆栈跟踪:

 SEVERE [http-nio-8084-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [com.api.customer.JerseyConfig] in context with path [] threw exception [java.lang.NullPointerException] with root cause
    java.lang.NullPointerException
        at org.glassfish.jersey.server.validation.internal.InjectingConstraintValidatorFactory.getInstance(InjectingConstraintValidatorFactory.java:37)
        at org.hibernate.validator.internal.engine.constraintvalidation.ClassBasedValidatorDescriptor.newInstance(ClassBasedValidatorDescriptor.java:75)
        at org.hibernate.validator.internal.engine.constraintvalidation.AbstractConstraintValidatorManagerImpl.createAndInitializeValidator(AbstractConstraintValidatorManagerImpl.java:89)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorManagerImpl.getInitializedValidator(ConstraintValidatorManagerImpl.java:117)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:152)
        at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:54)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:75)
        at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:130)
        at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:123)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:555)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:518)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:488)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:450)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:400)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedAnnotatedObjectForCurrentGroup(ValidatorImpl.java:629)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:590)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateParametersInContext(ValidatorImpl.java:880)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:283)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateParameters(ValidatorImpl.java:235)
        at org.glassfish.jersey.server.validation.internal.DefaultConfiguredValidator.onValidate(DefaultConfiguredValidator.java:154)
        at org.glassfish.jersey.server.validation.internal.ValidationInterceptorExecutor.proceed(ValidationInterceptorExecutor.java:90)
        at org.glassfish.jersey.server.validation.internal.DefaultConfiguredValidator.validateResourceAndInputParams(DefaultConfiguredValidator.java:122)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:114)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:475)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:397)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81)
        at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:255)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:234)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
        at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205

我已经查看了我的代码和配置,但无法确定此 NullPointerException 的来源。

我尝试过什么:

  1. 验证了我的 JSON 请求正文和控制器设置
  2. 已检查Jersey应用程序的配置
  3. 检查了我的pom.xml中的依赖关系是否存在任何潜在冲突

其他信息:

泽西岛版本:2.34

Java版本:1.8

服务器:Tomcat 9.0

依赖:

javax.servlet、jersey-container-servlet-core、jersey-media-json-jackson、jersey-hk2、weld-se-core、junit-jupiter、sqlite-jdbc、






jersey-bean-validation

我将不胜感激任何关于为什么会发生此 NullPointerException 以及如何解决它的任何指导或见解。提前感谢您的帮助。

Java JSON 验证 nullPointerException Jersey

评论

0赞 stelios.anastasakis 9/12/2023
这里没有太多可处理的问题,但通常依赖项可以解决这个问题。对于初学者,我会尝试更新版本的 Glassfish
0赞 Narayan Ghimire 9/12/2023
@stelios.anastasakis,你能告诉我我应该为更新的版本使用哪些依赖项吗?你是说球衣版吗?我也使用过 Jersey 版本 3,在这种情况下,我也遇到了同样的问题。基本上,我在我的 Macbook M2 上运行它(这是一个问题吗?
0赞 jan.supol 9/12/2023
日志中是否有警告,指出“资源类的查找和初始化失败”?
0赞 Narayan Ghimire 9/12/2023
@jan.supol:不,我没有那种警告。每次我发布关于该问题时,我都会看到错误
1赞 Narayan Ghimire 9/13/2023
我现在已经修复了它,它正在工作。Mac M2、Tomcat 9.0 似乎有问题。现在我正在使用 glassfish 服务器,jdk 11 正在工作。

答: 暂无答案