Computing Magazine

Create Json Rest Service in RestEasy

Posted on the 09 May 2013 by Abhishek Somani @somaniabhi
Creating rest web services with rest easy is very simple , you can read more about this in this post. Now if you want to add json support in rest you have to add following dependency in your pom.xml
<dependency>
       <groupId>org.jboss.resteasy</groupId>
       <artifactId>resteasy-jackson-provider</artifactId>
       <version>2.3.2.Final</version>
    </dependency>
And this is the Rest class
@Path("/test")
 @POST
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public Response getResult(Input in) {
  Output out = new Output();
  out.setMessage("success");
  return Response.status(Status.OK).entity(out).build();
 }

Where Input.java and Output.java will be beans which will be converted in to/from json by jackson. If you forget to add dependency you might get error like this in tomcat :
EVERE: Failed executing POST /test
org.jboss.resteasy.spi.BadRequestException: Could not find message body reader for type: class com.onlinehttpclient.bean.Input of content type: application/json;charset="utf-8"
 at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:153)
 at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:124)
 at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:147)
 at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257)
 at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222)
 at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211)
 at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525)
 at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502)
 at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119)
 at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208)
 at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55)
 at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at com.onlinehttpclient.filter.CrawlServlet.doFilter(CrawlServlet.java:105)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
 at java.lang.Thread.run(Thread.java:722)



Post your comments !! Create Json Rest Service in RestEasy

Back to Featured Articles on Logo Paperblog