1

please let me begin by telling you what I intend to do. I intend to follow through the tutorial on 'Building a RESTful Web Service' exactly as is mentioned in the Springframework's website http://spring.io/guides/gs/rest-service/

My problem is setting up the environment so I can launch a first prototype. I am reliant on the Eclipse IDE on a Windows 7 64-bit machine. The project is based on a 'gradle' build file. I get the error when I try to run the project using the build.gradle. Here is how I am trying to run the project.

At the project explorer window, right-click on the project>Run As>Gradle build>tomcatrun

The build.gradle file is as follows:

apply plugin: 'war'
apply plugin: 'tomcat'
apply plugin: 'java'
apply plugin: 'propdeps'
apply plugin: 'propdeps-maven'
apply plugin: 'propdeps-idea'
apply plugin: 'propdeps-eclipse'
apply plugin: 'eclipse'
apply plugin: 'idea'

buildscript {
  repositories {
    mavenCentral()
    maven {
      url "http://download.java.net/maven/2"
    }
    maven { url 'http://repo.spring.io/plugins-release' }
  }

  dependencies {
    classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
    classpath 'org.springframework.build.gradle:propdeps-plugin:0.0.1'
  }
}


repositories {
    mavenCentral()
}

dependencies {
    def tomcatVersion = '7.0.12'
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
            "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}") {
      exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }

    compile 'org.springframework:spring-core:3.2.3.RELEASE'
    compile 'org.springframework:spring-webmvc:3.2.3.RELEASE'
    compile 'com.jayway.jsonpath:json-path:0.8.1'

    compile 'org.slf4j:slf4j-api:1.7.5'
    runtime 'org.slf4j:slf4j-jdk14:1.7.5'
    runtime 'com.fasterxml.jackson.core:jackson-core:2.2.2'
    runtime 'com.fasterxml.jackson.core:jackson-databind:2.2.2'
    runtime 'javax.xml.bind:jaxb-api:2.2.9'

    provided 'javax.servlet:javax.servlet-api:3.0.1' 

    testCompile 'com.jayway.jsonpath:json-path-assert:0.8.1'
    testCompile 'org.springframework:spring-test:3.2.3.RELEASE'
    testCompile 'junit:junit:4.+'
    testCompile "org.mockito:mockito-all:1.9.5"

}

task wrapper(type: Wrapper) {
    gradleVersion = '1.6'
}

tomcatRunWar.contextPath = ''

I think the problem is in correctly defining dependency with the package. 'javax.servlet:javax.servlet-api:3.0.1'

Some other references on the web mention supplying the 'provided' attribute in the build file. That keyword is indeed present in the file. I tried looking up how to do the same on some gradle documentation (in case something was missing) but I can't locate the problem. The tutorial assumes that you're running the build in a linux shell. I think there's some thing about using the eclipse IDE that triggers the error. Any help will be appreciated. Here is the WebApplicationInitializer file:

// {!begin top}
package com.yummynoodlebar.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;
import java.util.Set;

public class WebAppInitializer implements WebApplicationInitializer {

  private static Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class);
// {!end top}

  // {!begin onStartup}
  @Override
  public void onStartup(ServletContext servletContext) {
    WebApplicationContext rootContext = createRootContext(servletContext);

    configureSpringMvc(servletContext, rootContext);
  }
  // {!end onStartup}

  // {!begin createRootContext}
  private WebApplicationContext createRootContext(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.register(CoreConfig.class);
    rootContext.refresh();

    servletContext.addListener(new ContextLoaderListener(rootContext));
    servletContext.setInitParameter("defaultHtmlEscape", "true");

    return rootContext;
  }
  // {!end createRootContext}

  // {!begin configureTop}
  private void configureSpringMvc(ServletContext servletContext, WebApplicationContext rootContext) {
    AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
    mvcContext.register(MVCConfig.class);

    mvcContext.setParent(rootContext);

    // {!end configureTop}
    // {!begin configureBottom}
    ServletRegistration.Dynamic appServlet = servletContext.addServlet(
        "webservice", new DispatcherServlet(mvcContext));
    appServlet.setLoadOnStartup(1);
    Set<String> mappingConflicts = appServlet.addMapping("/");

    if (!mappingConflicts.isEmpty()) {
      for (String s : mappingConflicts) {
        LOG.error("Mapping conflict: " + s);
      }
      throw new IllegalStateException(
          "'webservice' cannot be mapped to '/'");
    }
    // {!end configureBottom}
  }
}

Thanks a lot!

apil.tamang
  • 2,545
  • 7
  • 29
  • 40
  • possible duplicate of [SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer](http://stackoverflow.com/questions/15328363/springservletcontainerinitializer-cannot-be-cast-to-javax-servlet-servletcontain) – greg-449 Feb 11 '14 at 18:34
  • The one above references configuration on a pom.xml file (for maven builds). Also, that file references a number of servlet-api dependencies which I'm not sure I can correctly infer. – apil.tamang Feb 11 '14 at 21:43
  • It's possible the Tomcat libs you are using come with a different `servlet-api` version then what `spring-webmvc` was compiled with. Look into that. – Sotirios Delimanolis Feb 12 '14 at 07:01
  • Thanks for the opinions, guys! I used the Tomcat version that the original gradle file pointed to (v7.0.42). No can work! At any rate, I completely scraped my entire OS, switched to Kubuntu, and hoping to stay on the Linux platform for all development from now on. I needed a break from my cluttered system anyways. Let's hope for the best. – apil.tamang Feb 13 '14 at 13:08
  • possible duplicate of [ServletDispatcher cannot be cast to Javax.servlet.Servlet exception in my spring project](http://stackoverflow.com/questions/11704069/servletdispatcher-cannot-be-cast-to-javax-servlet-servlet-exception-in-my-spring) – Rugal Mar 18 '14 at 14:37

1 Answers1

6

I got this error, while configuring my Spring Based Web Application in Tomcat 7(using Maven).In my pom.xml the following dependency was there:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>

Due to which the javax.servlet jar is getting included in the deployment artifact. Upon providing the scope "provided" to the above mentioned dependency I was able to fix the issue. Hope this helps....

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>
Subhankar
  • 131
  • 2
  • 4