3

I have been struggling with this for hours now. I'm working on JAX-RS with jersey. Everything was going fine until I had to return JSON at one place. I know there must be a jersey-media-moxy library to be there in order to binding your Java objects to JSON format. I downloaded several JSON libraries one after another starting from Jersey-media-moxy to Google's genson library. But no success yet. With Jersey-media-moxy which I downloaded from http://www.java2s.com/Code/Jar/j/Downloadjerseymediamoxy23jar.htm, I get the following error while running the webservice.

SEVERE: StandardWrapper.Throwable
java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.PropertiesHelper.getValue(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
    at org.glassfish.jersey.moxy.json.MoxyJsonFeature.configure(MoxyJsonFeature.java:67)
    at org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:714)
    at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:644)
    at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:829)
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:453)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:390)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:172)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:364)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

Jan 22, 2016 6:44:46 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet JAVA WS
java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.PropertiesHelper.getValue(Ljava/util/Map;Ljavax/ws/rs/RuntimeType;Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Class;)Ljava/lang/Object;
    at org.glassfish.jersey.moxy.json.MoxyJsonFeature.configure(MoxyJsonFeature.java:67)
    at org.glassfish.jersey.model.internal.CommonConfig.configureFeatures(CommonConfig.java:714)
    at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:644)
    at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:829)
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:453)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:184)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:350)
    at org.glassfish.jersey.server.ApplicationHandler$3.call(ApplicationHandler.java:347)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:255)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:347)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:390)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:172)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:364)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:817)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

If I change the @Produces() annotation with MediaType.APPLICATION_XML, every thing works fine but as soon as I change it to MediaType.APPLICATION_JSON, I get the error.

It is a non-maven project where I added the jars via Project-> Properties. I tried other JSON libraries like Jetty, Jackson but not a single one worked. Libraries other than Jersey-media-moxy thorws the MessageBodyWriter not found exception. What is it I'm doing wrong? Is it something with the dependecies? Here is my project structure:

Project Structure

Any help in this regard would be highly appreciated. Thanks in Advance.

Syed Danish Ali
  • 381
  • 1
  • 6
  • 22
  • Your MOXy (2.3) version is lower than your Jersey version (2.22.1). They are incompatible. You need to update MOXy. – Paul Samsotha Jan 22 '16 at 02:15
  • Thanks for the comment. This is the latest version I've found. Since I've created a non-maven project, I really dont know how to add external libraries except the Jar option. – Syed Danish Ali Jan 22 '16 at 02:23
  • Instead of MOXy, you can use Jackson, as mentioned [here](http://stackoverflow.com/a/30424031/2587435). You will need to get rid of the MOXy jar you have. The Jackson jars in the link aren't the latest, but they should work for you. Here are jars for [MOXy](http://stackoverflow.com/a/26706617/2587435). Not the latest version, but it should work. Yout problem is with MOXy being a _pre_-2.8 version – Paul Samsotha Jan 22 '16 at 02:47
  • As an aside, you may want to take some time to learn a build framework that takes car of dependencies for you. Trying to find jars yourself, you will often run into problems like this, especially if your information is coming from different various tutorials. – Paul Samsotha Jan 22 '16 at 02:49
  • That is quite an advice, Sir. Can you please guide me a bit? I've worked with gradle build framework but that is really a different thing if compared to maven. At first, I started working with maven but due to some compile time and runtime dependency errors, I preferred to shift back to non-maven build. A direction from you, would be a great help. – Syed Danish Ali Jan 22 '16 at 04:41
  • If you use Gradle, you only need [two dependencies](http://stackoverflow.com/a/27890414/2587435). If you don't want to use Gradle, then the two link I provided above have images for both Jackson and MOXy jars. You should use one or the other. Take your pick. Personally, I'd say if you are comfortable with Gradle, then use it. – Paul Samsotha Jan 22 '16 at 04:49

3 Answers3

1

I have been experiencing the same problem and took me a long while to get rid of this.

The basic issue with this problem is missing couple of jar which are requested by jersey-media-moxy-2.27.jar jersey-media-moxy-2.27 jar internally uses some more API's which are required during the runtime.

To solve this, refer the snap attached and make sure you have all the jars in your project (version matters) image

Suggestion: If you are using Eclipse IDE, thought the server is "Started", try to have look at the console log while you are starting up the server (in my case, its Tomcat). there shouldn't be any error messages.

Ppm
  • 11
  • 2
0

Ensure you jersey.media jar has exactly same version as other jersey jar

compile "org.glassfish.jersey.media:jersey-media-json-jackson:{$version}

$version should be same for all jars

sudhir sharma
  • 904
  • 6
  • 3
0

In my case it happens because of:

<dependency>
    <groupId>org.glassfish.jersey.connectors</groupId>
    <artifactId>jersey-apache-connector</artifactId>
    <version>${jersey.version}</version>
</dependency>

where ${jersey.version} wasn't the same as other jersey dependencies

Taraskin
  • 561
  • 9
  • 15