0

Why do I get java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest when I add @Controller to DummyController class?

it.cspnet.firstspringmvc.controller.Main

   public static void main(String args[]) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("jpaContext.xml");
        Servizi servizi = ctx.getBean(Servizi.class);
        Utente utente = new Utente();
        utente.setUserName("test");
        utente.setPassword("test");
        Utente utenteInDb = servizi.login(utente);

        for (Ordine ordine : utenteInDb.getOrdini()) {
            System.out.println("ordine: " + ordine);
        }
    }

it.cspnet.firstspringmvc.controller.DummyController

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletRequest;

@Controller
public class DummyController {

    @RequestMapping(value = "/dummy", method = {RequestMethod.GET})
    public String get(Model model, HttpServletRequest request) {
        return "dummy";
    }
}

When I remove the @Controller annotation from DummyController then main prints out the example's fine but if I put it back in then it throws:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest at java.lang.Class.getDeclaredMethods0(Native Method)

I'm using this project:

https://github.com/ivansaracino/spring-mvc-jparepository-example.git

All I've done is add Main and DummyController

6
  • what building system do you use ? maven ? Commented Dec 30, 2014 at 17:11
  • 1
    When adding @Controller the bean is detected and loaded, when removed it isn't loaded and when it isn't loaded the class HttpServletRequest isn't needed. Commented Dec 30, 2014 at 18:59
  • Because the dependcy is sopped provided. However it doesn't make sense to have a @Controller in a standalone program. Commented Dec 31, 2014 at 9:33
  • No it isn't as it tells you nothing for a web application, you should use your tests for that. Commented Dec 31, 2014 at 9:42
  • As stated you should use your tests for that... When including the servlet api (compile scope) you will run into problems with your server as you aren't allowed to include the servlet api in your application. Commented Dec 31, 2014 at 9:44

3 Answers 3

4

You are probably missing right dependencies, like:

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

Please note that your version could be 2.5, 3.0 or 3.1 - it depends on application server you are using. Also when you want to create executable war you should probably not use provided scope (depending on your servlet container).

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I was offline. What is the status ? Works everything ? If not I would suggest you to use some of the spring boot manuals. You can find a lot of them on spring.io site. Althought it is tempting to try to configure all by yourself, the spring boot will save you a lot of time. Recently we migrated all of our old spring projects to spring boot and we will never go back.
1

Your dependency scope is 'provided', so when you build the war this dependency won't be added to the class path! Make sure the dependency exists on App Server lib path.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.