I'm trying to run a Jersey REST Service using a Jetty Server. When I run in IntelliJ I get the following error:
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:309)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:338)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:171)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:363)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
etc...
at service.WebserviceRunner.start(WebserviceRunner.java:37)
If I run the same project in Eclipse It runs fine. Why would the IDE Make a difference?
WebserviceRunner.java
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.servlet.ServletContainer;
public class WebserviceRunner {
private Server server;
private int port;
private String host;
public WebserviceRunner(String host, int port) {
this.server = new Server(port);
this.host = host;
this.port = port;
}
public void start() throws Exception {
HandlerCollection handlers = new HandlerCollection();
WebAppContext handler = new WebAppContext();
handler.setContextPath("/");
handler.setResourceBase("./");
handler.setClassLoader(Thread.currentThread().getContextClassLoader());
ServletHolder restServlet = handler.addServlet(ServletContainer.class, "/*");
restServlet.setInitOrder(0);
restServlet.setInitParameter(ServerProperties.PROVIDER_PACKAGES,"resource");
handlers.addHandler(handler);
server.setHandler(handlers);
server.start();
System.out.println("API started... at 'http://" + getHostAndPort() + "'");
}
private String getHostAndPort() {
return host + ":" + port;
}
public static void main(String[] args) throws Exception {
new WebserviceRunner("localhost", 8315).start();
}
}
build.gradle
apply plugin: 'java'
apply plugin: 'jetty'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
compile 'org.glassfish.jersey.core:jersey-server:2.17'
compile 'org.glassfish.jersey.containers:jersey-container-servlet-core:2.17'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'javax.inject:javax.inject:1'
compile 'com.google.code.gson:gson:2.2.2'
compile 'com.google.guava:guava:10.0'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
def jettyVersion = '7.2.2.v20101205';
compile "org.eclipse.jetty:jetty-server:${jettyVersion}"
compile "org.eclipse.jetty:jetty-webapp:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlet:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlets:${jettyVersion}"
compile "org.eclipse.jetty:jetty-annotations:${jettyVersion}"
compile 'org.mortbay.jetty:jsp-2.1-glassfish:2.1.v20100127'
compile 'javax.servlet:jstl:1.2'
compile 'javax.servlet:javax.servlet-api:3.0.1'
}
Note: I think this is something to do with having two different versions of the same Application class on my classpath. Why is this when I'm only importing one?