DSL-Json 使用 RESTEasy - 获取 NoMessageBodyWriterFoundFailure 错误

DSL-Json with RESTEasy - Getting NoMessageBodyWriterFoundFailure error

我正在将 RestEasy 与 Dsl-JSON 集成,但出现以下错误:

Failed executing GET /json/sample/get org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type

我的代码如下:

@Path("/json/sample")
public class JSONService {

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public Sample getProductInJSON() {

        Sample product = new Sample();
        product.setName("abcd");
        product.setAge(12);

        return product; 
    }
}

我的 pom 看起来像这样:

<dependencies>
    <dependency>
        <groupId>com.dslplatform</groupId>
        <artifactId>dsl-json</artifactId>
        <version>1.0.0</version>
    </dependency>

    <dependency>
        <groupId>com.dslplatform</groupId>
        <artifactId>dsl-json-processor</artifactId>
        <version>0.9</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.16.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-client</artifactId>
        <version>3.0.16.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</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>    

相同的休息端点与 Jackson 一起工作,但我想使用 DSL-Json 并将其与 RestEasy 集成。 mvn 构建成功,我看到正在为所有 @CompiledJson objects.

生成 ExternalSerialization class

我在 headers 中超过了 application/json。

任何线索将不胜感激。

谢谢:)

messageBodyReader class如下:

@Provider
@Produces("application/json")
public class MyMessageBodyWriter implements MessageBodyWriter<Object> {

    @Override
    public boolean isWriteable(Class type, Type genericType,
            Annotation[] annotations, MediaType mediaType) {
        System.out.println("isWriteable called...");
        return  Sample.class == type; 
    }

    @Override
    public long getSize(Object arg0, Class<?> arg1, Type arg2,
            Annotation[] arg3, MediaType arg4) {
        return -1;
    }


    @Override
    public void writeTo(Object t, Class<?> type, Type genericType,
            Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, Object> httpHeaders,
            OutputStream entityStream) throws IOException,
            WebApplicationException {

        @SuppressWarnings("resource")
        JsonWriter writer = new JsonWriter();
        entityStream.write(writer.getByteBuffer());     
    }
}

而 MyMessageBodyReader 是这样的:

@Provider
public class MyMessageBodyReader extends DslJson implements    MessageBodyReader<Object> {

    @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 {
        return "Successfuly read ";
    }

    @Override
    public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
            MediaType arg3) {
        // TODO Auto-generated method stub
        return false;
    }
}

当其余端点被击中时,我无法调用这些提供程序,我尝试使用
应用 Class getClasses 方法:

public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(MyMessageBodyReader.class);
    classes.add(MyMessageBodyWriter.class);
    return classes;
}

但它没有注册这些 classes。

RestEasy 配置:web.xml 文件

    <context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>com.x.y.rest.JSONService</param-value>
</context-param>

<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

您可以在 Revenj library 查看 DSL-JSON 的 Spring 集成。 它还对 DslJson 进行了包装,因此它可以直接访问读取器和写入器。

我没有使用 Resteasy 的经验,但是如果您在 Github 上的某处在线有代码,我可以稍后查看。