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

时间:2016-05-09 18:16:14

标签: java json resteasy dsl

我将RestEasy与Dsl-JSON集成,但我收到以下错误:

  

执行GET / json / sample / get org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure失败:找不到类型为

的响应对象的MessageBodyWriter

我的代码如下:

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

同样的休息端点正在与杰克逊合作,但我想使用DSL-Json并将其与RestEasy集成。 mvn构建是succcesful,我看到为所有ExternalSerialization对象生成了@CompiledJson类。

我在标题中传递了application / json。

任何线索都会非常感激。

谢谢:)

messageBodyReader类如下:

@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;
    }
}

当其他端点被击中时,我无法调用这些提供者,我尝试使用
Application Class getClasses方法:

public Set<Class<?>> getClasses() {
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(MyMessageBodyReader.class);
    classes.add(MyMessageBodyWriter.class);
    return 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>

1 个答案:

答案 0 :(得分:0)

您可以在Revenj library查看针对DSL-JSON的Spring集成。 它还有一个围绕DslJson的包装器,因此它可以直接访问读者和编写者。

我没有Resteasy的经验,但是如果你在Github网上有代码,我可以稍后再看看。