现NoMessageBodyWriterFoundFailure

现NoMessageBodyWriterFoundFailure

本文介绍了DSL-Json与RESTEasy-出现NoMessageBodyWriterFoundFailure错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm integrating RestEasy with Dsl-JSON but I'm getting the following error:

我的代码如下:

@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看起来像这样:

My pom looks like this:

<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对象生成ExternalSerialization类.

The same rest endpoint was working with Jackson but I want to use DSL-Json and integrate it with RestEasy. The mvn build is succcesful and I see the ExternalSerialization class being generated for all the @CompiledJson objects.

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

I'm passing application/json in the headers.

任何线索都将不胜感激.

Any leads would be greatly appreciated.

谢谢:)

messageBodyReader类如下:

The messageBodyReader class is as below:

@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就像这样:

And MyMessageBodyReader is like this:

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

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

I'm unable to invoke these Providers when the rest endpoint is hit, I tried using
Application Class getClasses method:

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

但是它没有注册这些类.

But it's not registering these classes.

RestEasy配置:web.xml文件

RestEasy Configuration : web.xml file

    <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库.它还具有DslJson的包装,因此可以直接访问读取器和写入器.

You can take a look at the Spring integration for DSL-JSON at the Revenj library.It also has a wrapper around DslJson so it can access readers and writers directly.

我没有使用Resteasy的经验,但是如果您在Github上的某个地方在线有代码,我以后可以看一下.

I don't have experience with Resteasy, but if you have code somewhere online on Github, I can take a look at it later.

这篇关于DSL-Json与RESTEasy-出现NoMessageBodyWriterFoundFailure错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 07:00
查看更多