DSL-Json with RESTEasy - Getting NoMessageBodyWrit

2019-09-02 16:58发布

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

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

My code is as below:

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

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>    

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.

I'm passing application/json in the headers.

Any leads would be greatly appreciated.

Thank you :)

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());     
    }
}

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

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

1条回答
▲ chillily
2楼-- · 2019-09-02 17:43

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.

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

查看更多
登录 后发表回答