0

我尝试用 Json 字符串填充 Java 对象,它可以为同一字段提供不同类型的变量名。这就是我尝试使用@JsonAlias 的原因。不幸的是,由于未知的原因,我还不能让它工作..

这是我的例子:

Java 对象:

import com.fasterxml.jackson.annotation.JsonAlias;

public class ExampleClass {

    @JsonAlias({"test_1", "1"})
    private String test1;

    @JsonAlias({"test_2", "2"})
    private String test2;

    public String getTest1() {
        return test1;
    }

    public void setTest1(String test1) {
        this.test1 = test1;
    }

    public String getTest2() {
        return test2;
    }

    public void setTest2(String test2) {
        this.test2 = test2;
    }
}

Json,我尝试反序列化:

ObjectMapper mapper = new ObjectMapper();

ExampleClass javaObject = mapper.readerFor(ExampleClass.class).readValue("{\"test_1\": \"heyo\", \"test_2\":  \"heyo2\" }");

在字段“test1”上方的@JsonAlias 注释中定义“test_1”后,Jackson 应该找到并填写该字段。但是我得到了以下错误:

17:02:34,653 ERROR [stderr] (default task-3) com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "test_1" (class ch.ejb.ExampleClass), not marked as ignorable (2 known properties: "test2", "test1"])

编辑:我的 Maven 依赖项:

        <dependencies>

            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </dependency>
            <dependency><!-- add slf4j VYTH -->
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
                <version>1.7.9</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>3.9</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>3.6.0.Final</version>

            </dependency>
            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itextpdf</artifactId>
                <version>5.5.7</version>
            </dependency>
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.23</version>
            </dependency>
            <dependency>
                <groupId>com.itextpdf.tool</groupId>
                <artifactId>xmlworker</artifactId>
                <version>5.5.7</version>
            </dependency>
            <dependency>
                <groupId>javax.ejb</groupId>
                <artifactId>javax.ejb-api</artifactId>
                <version>3.2.2</version>
            </dependency>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.0</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.resteasy</groupId>
                <artifactId>resteasy-jaxrs</artifactId>
                <version>3.0.19.Final</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>0.9.1</version>
                <exclusions>
                    <exclusion>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-databind</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-text</artifactId>
                <version>1.9</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>2.8.7</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.12.0</version>
            </dependency>
            <dependency>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-jaxrs2</artifactId>
                <scope>compile</scope>
                <version>2.0.10</version>
            </dependency>
            <dependency>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-annotations</artifactId>
                <version>2.0.10</version>
            </dependency>
            <dependency>
                <groupId>org.webjars</groupId>
                <artifactId>swagger-ui</artifactId>
                <version>2.1.4</version>
            </dependency>
            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>4.9.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>3.12.0</version>
            </dependency>
        </dependencies>

错误代码:


13:36:10,858 ERROR [stderr] (default task-6) com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "test_1" (class ch.rest.opk.ExampleClass), not marked as ignorable (2 known properties: "test2", "test1"])
13:36:10,859 ERROR [stderr] (default task-6)  at [Source: {"test_1": "heyo", "test_2":  "heyo2" }; line: 1, column: 13] (through reference chain: ch.rest.opk.ExampleClass["test_1"])
13:36:10,860 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:63)
13:36:10,861 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:834)
13:36:10,862 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1093)
13:36:10,863 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1489)
13:36:10,864 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1467)
13:36:10,865 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:282)
13:36:10,866 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
13:36:10,867 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1626)
13:36:10,868 ERROR [stderr] (default task-6)    at com.fasterxml.jackson.core.jackson-databind//com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1220)
13:36:10,869 ERROR [stderr] (default task-6)    at deployment.opk-backend.war//ch.rest.opk.LimitenServices.getLimitenExcel(LimitenServices.java:153)
13:36:10,870 ERROR [stderr] (default task-6)    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:36:10,870 ERROR [stderr] (default task-6)    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
13:36:10,871 ERROR [stderr] (default task-6)    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
13:36:10,872 ERROR [stderr] (default task-6)    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
13:36:10,873 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
13:36:10,874 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
13:36:10,875 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
13:36:10,876 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
13:36:10,877 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:406)
13:36:10,879 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:213)
13:36:10,880 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:228)
13:36:10,882 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
13:36:10,883 ERROR [stderr] (default task-6)    at org.jboss.resteasy.resteasy-jaxrs//org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
13:36:10,884 ERROR [stderr] (default task-6)    at javax.servlet.api//javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
13:36:10,885 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
13:36:10,886 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
13:36:10,887 ERROR [stderr] (default task-6)    at deployment.opk-backend.war//ch.rest.CorsResponseFilter.doFilter(CorsResponseFilter.java:56)
13:36:10,887 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
13:36:10,888 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
13:36:10,889 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
13:36:10,890 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
13:36:10,891 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:65)
13:36:10,892 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
13:36:10,893 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
13:36:10,894 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:36:10,896 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
13:36:10,897 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
13:36:10,897 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:36:10,898 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
13:36:10,899 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
13:36:10,900 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
13:36:10,901 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
13:36:10,902 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
13:36:10,903 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
13:36:10,905 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:36:10,906 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
13:36:10,907 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:36:10,908 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
13:36:10,909 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
13:36:10,910 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
13:36:10,911 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
13:36:10,912 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
13:36:10,913 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
13:36:10,914 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
13:36:10,915 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
13:36:10,916 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
13:36:10,917 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1501)
13:36:10,918 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1501)
13:36:10,919 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1501)
13:36:10,920 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1501)
13:36:10,921 ERROR [stderr] (default task-6)    at org.wildfly.extension.undertow//org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1501)
13:36:10,922 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
13:36:10,924 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
13:36:10,925 ERROR [stderr] (default task-6)    at io.undertow.servlet//io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
13:36:10,926 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.Connectors.executeRootHandler(Connectors.java:330)
13:36:10,926 ERROR [stderr] (default task-6)    at io.undertow.core//io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
13:36:10,927 ERROR [stderr] (default task-6)    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
13:36:10,928 ERROR [stderr] (default task-6)    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
13:36:10,929 ERROR [stderr] (default task-6)    at java.base/java.lang.Thread.run(Thread.java:834)

4

2 回答 2

0

尝试使用@JsonPropery而不是@JsonAlias

public class ExampleClass {

    @JsonProperty("test_1")  //Change 
    private String test1;

    @JsonProperty("test_2")  //Change
    private String test2;

    public String getTest1() {
        return test1;
    }

    public void setTest1(String test1) {
        this.test1 = test1;
    }

    public String getTest2() {
        return test2;
    }

    public void setTest2(String test2) {
        this.test2 = test2;
    }
}

请参阅Javadoc

于 2022-02-01T13:08:05.000 回答
0

我终于找到了解决方案。问题是,我们的 JBoss Wildfly 服务器也使用了 Jackson 库,它们引起了冲突。可能是因为版本,大约是 1.*

一旦我排除了那些旧的杰克逊库,@JsonAlias 就会按预期工作:

<jboss-deployment-structure>
<deployment>
    <exclusions>
        <module name="com.fasterxml.jackson.core.jackson-core" />
        <module name="com.fasterxml.jackson.core.jackson-databind" />
        <module name="com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider" />
        <module name="org.jboss.resteasy.resteasy-jackson2-provider" />
    </exclusions>
</deployment>
于 2022-02-09T09:49:59.547 回答