facebook

ME 2015 how to get a REST service to use json

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #392485 Reply

    mongonv
    Participant

    I have been going thru the ME tutorial for creating REST services and it shows how to do XML and that works. But if I try to use JSON as the mime type of the produces I cannot get past the following error:

    Jun 03, 2015 3:34:21 PM com.sun.jersey.spi.container.ContainerResponse write
    SEVERE: A message body writer for Java class java.util.ArrayList, and Java type java.util.List<com.myeclipseide.ws.Customer>, and MIME media type application/json was not found
    Jun 03, 2015 3:34:21 PM com.sun.jersey.spi.container.ContainerResponse write
    SEVERE: The registered message body writers compatible with the MIME media type are:
    */* ->
    com.sun.jersey.core.impl.provider.entity.FormProvider
    com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
    com.sun.jersey.core.impl.provider.entity.StringProvider
    com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
    com.sun.jersey.core.impl.provider.entity.FileProvider
    com.sun.jersey.core.impl.provider.entity.InputStreamProvider
    com.sun.jersey.core.impl.provider.entity.DataSourceProvider
    com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
    com.sun.jersey.core.impl.provider.entity.ReaderProvider
    com.sun.jersey.core.impl.provider.entity.DocumentProvider
    com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
    com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
    com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
    com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
    com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General

    I have been searching for anything on this for hours and have not found anything that works.

    If I change the code from the tutorial to:

    @Produces(“application/json”)

    @Path
    (“customers”)

    @Singleton

    public class CustomersResource

    and the related produces under the addCustomer it blows up in the Tomcat server.

    Any ideas on how to get ME 2015 to work would be greatly appreciated.

    #392509 Reply

    support-swapna
    Moderator

    mongonv,

    Please take a look at this tutorial for REST Web Services which uses Jackson libraries to convert objects to/from JSON : https://www.genuitec.com/creating-exploring-restws/

    Please also check this link for some more suggestions : http://stackoverflow.com/questions/13594945/how-correctly-produce-json-by-restful-web-service

    Hope this helps. Let us know if you have any questions.

    –Swapna
    MyEclipse Support

    #392513 Reply

    mongonv
    Participant

    Thanks Swapna,
    I will go thru that tutorial later today and let you know if it works.

    #392747 Reply

    mongonv
    Participant

    Hey Swapna,
    I have tried going thru the JSON tutorial you listed and it does not seem to cover something as I am still unable to get a working test using JSON as the producer. I have gone thru it multiple times trying different options and cannot find what it is missing.

    The error I am getting is:

    Jun 09, 2015 1:32:52 PM com.sun.jersey.server.impl.application.RootResourceUriRules <init>
    SEVERE: The ResourceConfig instance does not contain any root resource classes.
    Jun 09, 2015 1:32:52 PM org.apache.catalina.core.ApplicationContext log
    SEVERE: StandardWrapper.Throwable
    com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
    at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:103)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1182)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.access$600(WebApplicationImpl.java:161)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:698)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:695)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:197)

    Is there a sample project somewhere that can be pulled in with a working JSON setup?

    Thanks

    #392779 Reply

    support-swapna
    Moderator

    Ed,

    The error is because jersey can’t find a dependency package for your rest service declared.
    When adding the following init-param “com.sun.jersey.config.property.packages” in web.xml for Jersey to support Jackson object mapping, the param value should be your project package name.

    In the JSON tutorial it is : com.myeclipseide.rest.library but if your project package is different then it results in the error you are seeing.Please recheck the value in the web.xml and give it a try.

    Please find the sample project attached.

    Hope this helps. Let us know if you see any issues.

    –Swapna
    MyEclipse Support

    Attachments:
    You must be logged in to view attached files.
    #392784 Reply

    mongonv
    Participant

    Swapna,
    I checked and my project did have the correct settings in the web.xml (same as your sample project) and when I run your sample project I also get the same error, here is the full error stack:

    Jun 10, 2015 8:07:00 AM com.sun.jersey.server.impl.application.RootResourceUriRules <init>
    SEVERE: The ResourceConfig instance does not contain any root resource classes.
    Jun 10, 2015 8:07:00 AM org.apache.catalina.core.ApplicationContext log
    SEVERE: StandardWrapper.Throwable
    com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
    at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:103)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1182)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.access$600(WebApplicationImpl.java:161)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:698)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:695)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:197)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
    at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:438)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:287)
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:587)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:213)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:342)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:516)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    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:744)
    Jun 10, 2015 8:07:00 AM org.apache.catalina.core.StandardContext loadOnStartup
    SEVERE: Servlet /jsonProject threw load() exception
    com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
    at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:103)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1182)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.access$600(WebApplicationImpl.java:161)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:698)
    at com.sun.jersey.server.impl.application.WebApplicationImpl$12.f(WebApplicationImpl.java:695)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:197)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:695)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:690)
    at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:438)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:287)
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:587)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:213)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:342)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:516)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5231)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5518)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1247)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1897)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    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:744)

    What is missing in the project?

    Thanks

    #392839 Reply

    support-swapna
    Moderator

    Ed,

    Sorry that you are still seeing issues.

    When do you see the error? Is it in the console during the deployment ?

    I rechecked with the sample project in a new workspace and I do not see any issues at my end.
    Can you please switch to a new workspace, import the sample project, clean the MyEclipse Tomcat server of its previous deployments (clean(redeploy)) and check if the deployment works.

    Let us know how it works for you.

    –Swapna
    MyEclipse Support

    #392923 Reply

    mongonv
    Participant

    Swapna,
    Yes it seems to work if I wipe out the tomcat working files in the test workspace or use a new workspace. Have not tried it against one of my working projects, so will have to see if there is a conflict going there.

    Would it be the use of the jackson jars in a project that would cause the conflict? Just asking if you have an idea on that.

    Thanks

    #393050 Reply

    support-swapna
    Moderator

    Ed,

    It might be an issue with the class loader not loading the servlet classes once they are updated. Clean redeploy would remove all traces of the project and deploy. The project deployment works for me in an existing workspace on Tomcat which has couple of other projects deployed as well without the need for a clean redeploy.
    I am not sure about the jackson jars conflict as I don’t see it at my end.

    Let us know if you have any questions.

    –Swapna
    MyEclipse Support

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: ME 2015 how to get a REST service to use json

You must be logged in to post in the forum log in