与dsl-json的resteasy集成 - 无法注册读者

时间:2016-04-07 08:21:10

标签: java json rest resteasy dsl

我正在尝试将dsl-json与resteasy集成用于de / serialization。但它会引发异常

 java.io.IOException: Unable to find reader for provided type: class com.x.y.api.v1.resource.A and fallback serialization is not registered.
17:04:51,705 ERROR [stderr] (default task-2) Found reader for: class com.x.y.api.v1.resource.A so try deserializing into that instead?
17:04:51,705 ERROR [stderr] (default task-2) Alternatively, try initializing system with custom fallback or register specified type using registerReader into class com.dslplatform.json.DslJson
17:04:51,705 ERROR [stderr] (default task-2)    at com.dslplatform.json.DslJson.showErrorMessage(DslJson.java:650)
17:04:51,706 ERROR [stderr] (default task-2)    at com.dslplatform.json.DslJson.deserialize(DslJson.java:980)
17:04:51,706 ERROR [stderr] (default task-2)    at com.x.y.provider.DSLJsonProvider.readFrom(DSLJsonProvider.java:41)
17:04:51,706 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.readFrom(AbstractReaderInterceptorContext.java:61)
17:04:51,706 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.interception.ServerReaderInterceptorContext.readFrom(ServerReaderInterceptorContext.java:60)
17:04:51,706 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
17:04:51,707 ERROR [stderr] (default task-2)    at org.jboss.resteasy.security.doseta.DigitalVerificationInterceptor.aroundReadFrom(DigitalVerificationInterceptor.java:34)
17:04:51,707 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
17:04:51,707 ERROR [stderr] (default task-2)    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59)
17:04:51,707 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:55)
17:04:51,707 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:151)
17:04:51,708 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:91)
17:04:51,708 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:114)
17:04:51,708 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
17:04:51,708 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
17:04:51,708 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
17:04:51,709 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:395)
17:04:51,709 ERROR [stderr] (default task-2)    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:202)
17:04:51,709 ERROR [stderr] (default task-2)    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
17:04:51,710 ERROR [stderr] (default task-2)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
17:04:51,710 ERROR [stderr] (default task-2)    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) 

我的DSLJsonProvider类看起来像这样。

    package com.x.y.provider;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Type;

    import javax.ws.rs.Consumes;
    import javax.ws.rs.Produces;
    import javax.ws.rs.WebApplicationException;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.MultivaluedMap;
    import javax.ws.rs.ext.MessageBodyReader;
    import javax.ws.rs.ext.MessageBodyWriter;
    import javax.ws.rs.ext.Provider;

    import com.dslplatform.json.DslJson;
    import com.dslplatform.json.JsonReader;
    import com.dslplatform.json.JsonReader.ReadObject;
    import com.dslplatform.json.JsonWriter;

    @SuppressWarnings("rawtypes")
    @Provider
    @Consumes({MediaType.APPLICATION_JSON, "text/json"})
    @Produces({MediaType.APPLICATION_JSON, "text/json"})
    public class DSLJsonProvider extends DslJson implements MessageBodyWriter, MessageBodyReader {

        @Override
        public boolean isReadable(Class type, Type genericType, Annotation[] annotations,
                javax.ws.rs.core.MediaType mediaType) {
            return true;
        }

        @SuppressWarnings("unchecked")
        @Override
        public Object readFrom(Class type, Type genericType, Annotation[] annotations, javax.ws.rs.core.MediaType mediaType,
                MultivaluedMap httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
            DslJson<Object> dslJson = new DslJson<Object>();
            try {
                dslJson.registerReader(type,null);//Here I need help
                return dslJson.deserialize(type, entityStream, new byte[1024]);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                entityStream.close();
            }
            return null;
        }

        @Override
        public boolean isWriteable(Class type, Type genericType, Annotation[] annotations,
                javax.ws.rs.core.MediaType mediaType) {
            return true;
        }

        @Override
        public long getSize(Object t, Class type, Type genericType, Annotation[] annotations,
                javax.ws.rs.core.MediaType mediaType) {
            return -1;
        }

        @Override
        @SuppressWarnings("resource")
        public void writeTo(Object t, Class type, Type genericType, Annotation[] annotations,
                javax.ws.rs.core.MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream)
                        throws IOException, WebApplicationException {
            JsonWriter writer = new JsonWriter();
            try {
//Need help to register writer
                writer.toStream(entityStream);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

编辑1: 我的pom.xml

<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>
    <groupId>com.x.y</groupId>
    <artifactId>project-web</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>project-web</name>
    <dependencies>
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-core</artifactId>
            <version>1.3.20.Final</version>
        </dependency>
        <dependency>
            <groupId>io.undertow</groupId>
            <artifactId>undertow-servlet</artifactId>
            <version>1.3.20.Final</version>
        </dependency>
        <dependency>
            <groupId>com.dslplatform</groupId>
            <artifactId>dsl-json</artifactId>
            <version>0.9.5</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.0.16.Final</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>3.0.16.Final</version>
        </dependency>
        <dependency>
            <groupId>com.dslplatform</groupId>
            <artifactId>dsl-json-java8</artifactId>
            <version>0.9.7</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <annotationProcessors>
                        <annotationProcessor>com.dslplatform.json.CompiledJsonProcessor</annotationProcessor>
                    </annotationProcessors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我在尝试注册读者和作家时有点受阻。任何领导都表示赞赏。

资源: dsl-json:https://github.com/ngs-doo/dsl-json

提前致谢。

2 个答案:

答案 0 :(得分:0)

错误消息显示它无法找到:com.x.y.api.v1.resource.A

的阅读器

要么您忘记将@CompiledJson添加到该课程中,要么因为某些原因未将其ServiceLoader选中。{/ p>

如果你想为JsonReader.ReadObject注册你自己的阅读器,你可以查看geom readers,虽然我认为你可能只是想为编译器添加注释以便选择它。

这些注释的处理器存在于一个单独的库(处理器)中,所以很可能你没有包含它或将它作为注释处理器添加。

你需要摆脱dslJson.registerReader(type,null);因为这将覆盖正确的读者。

我更新了dsl-json自述文件,因为我偶尔会遇到这样的错误。即:“在启动期间,DslJson通过ServiceLoader加载服务。为此,META-INF / services / com.dslplatform.json.Configuration必须与dsl_json.json.ExternalSerialization的内容一起存在,这是在编译步骤中创建的类。场景这个文件没有复制到APK或相应的jar / war文件。您可以通过在src / main / resources / META-INF / services下的项目中创建这样的文件来解决它,作为使用的包/构建工具的解决方法。“

我会对您的设置感兴趣,所以如果这样可以解决您的问题,请在存储库中提交一个问题,以便妥善解决。

答案 1 :(得分:0)

我遇到了同样的问题,并通过在反序列化类中添加默认构造函数进行了修复