4

I would like to put JSON dependency in the build.gradle, in order to fix the error MessageBodyWriter not found for media type=application/json. In my previous question I learned that it was very likely that I did not include JSON as dependency in my build.gradle file.

I added the dependency as shown below (line 8, last compile)

apply plugin: 'war'
apply plugin: 'jetty'

dependencies {
    compile fileTree(dir: 'lib', include: '**/*.jar')
    compile(project(":qa-common"))
    compile(project(":alm"))
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.3.10'
}

jettyRun {
    httpPort = 8080
    reload = 'automatic'
    scanIntervalSeconds = 2
    daemon = false
}

Now I am getting the error of Could not resolve all dependencies for configuration'

FAILURE: Build failed with an exception.

* What went wrong:
Could not resolve all dependencies for configuration ':qa-automation-console:compile'.
> Cannot resolve external dependency org.glassfish.jersey.media:jersey-media-json-jackson:2.3.10 because no repositories are defined.
  Required by:
  qaauto:qa-automation-console:unspecified

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 5.273 secs

I am checking whether I have included the correct version of gradle and Jersey (in web.xml I see 2.5 but 2.5 still gives the same error). In my jersey server side code the only package related to jersey was import org.glassfish.jersey.server.mvc.Viewable;

Anyone give me a clue what I need to add?

Community
  • 1
  • 1
jsh6303
  • 2,010
  • 3
  • 24
  • 50

1 Answers1

5

Define repositories in your build.gradle like following.

repositories {
      maven  {
          url "http://repo1.maven.org/maven2"
      }
}

Or following

repositories {
    mavenCentral()
}

Gradle Help Chapter 8. Dependency Management Basics 8.5. Repositories gives other examples.

You should also change your dependency according to existing versions. I linked version page since you request "version 2.3.10" which does not exist in maven repository.

dependencies {
    compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.5'
}
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
  • Thank you that is very helpful. Where/How can I find the exact URL that matches my maven repo? Is there anything in my local files that I need to find in order to match a 'version'? (like the maven2 refered in the above example?) – jsh6303 Aug 05 '15 at 20:52
  • 1
    You do not need to find exact url if you use standart maven central url. I added another example with mavenCentral() usage – Atilla Ozgur Aug 05 '15 at 20:55
  • I tried the both repos and it is getting another error `Could not resolve org.glassfish.jersey.media:jersey-media-json-jackson:2.5. ` `> Could not get resource https://repo1.maven.org/maven2/org/glassfish/jersey/media/jersey-media-json-jackson/2.5/jersey-media-json-jackson-2.5.pom.` `> Could not GET https://repo1.maven.org/maven2/org/glassfish/jersey/media/jersey-media-json-jackson/2.5/jersey-media-json-jackson-2.5.pom.` `> Connection to https://repo1.maven.org refused` – jsh6303 Aug 05 '15 at 21:06
  • 1
    @JiajuShen what version of Jersey are you using? You may just want to download all the jars, since that seems to be how you were already manging your jars. Sound like a firewall problem maybe, if the connection is refused. – Paul Samsotha Aug 06 '15 at 01:54
  • @peeskillet I just checked and I am using Jersey 2.19 – jsh6303 Aug 06 '15 at 16:18
  • @peeskillet Thank you. Do I need to put the dependencies/jars (ones mentioned in the first answer) in the same folder as the the one named "Web-INF/lib" directory? I ask this as my dependency (most of them) is in a different directory named "Gradle Dependencies" with a library icon instead of a folder icon. – jsh6303 Aug 06 '15 at 17:53
  • @JiajuShen They should do where ever the rest of your Jersey jars are. – Paul Samsotha Aug 06 '15 at 18:20
  • @peeskillet Turns out my gradle dependency folder and my jersey folder are separate(update: gradle dependency is a library that cites the content of jersey folder). Do you imply the jars mentioned in the answer you provided should be copied into jersey folder and then added into "Gradle dependency" library? – jsh6303 Aug 06 '15 at 18:39
  • 1
    @JiajuShen I mean the answer I linked to provides download links to a bunch of jars. With Maven or Gradle when we declare a dependency like `jersey-media-json-jackson`, the tool will download that jar as well as all the jars that it is dependent on. In the case of `jersey-media-json-jackson`, all the jars from that link would be pulled in. But since the downloads are not working on your Gradle (maybe denied by firewall), you need to download all those jars individually, and where ever you put all your other application jars, put those jars there. I'm not sure how else to explain it. – Paul Samsotha Aug 07 '15 at 01:44
  • @peeskillet Thanks very much. Actually I downloaded all the jar files you mentioned and without importing them in the gradle dependency library in eclipse (but they are put in the referenced library) and it returned the correct result. Plus I changed my gradle.build file without any other `compile`s other than the first three mentioned in the question. I believe the as long as the json jar is referenced it would solve the problem – jsh6303 Aug 07 '15 at 13:08