java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest添加到@Controller类时为什么会得到DummyController

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";
    }
}


当我从DummyController中删除@Controller批注时,main会打印出example的标签,但是如果我放回去,则会抛出:


  线程“主”中的异常java.lang.NoClassDefFoundError:
  javax / servlet / http / HttpServletRequest位于
  java.lang.Class.getDeclaredMethods0(本机方法)


我正在使用这个项目:

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

我所做的就是添加Main和DummyController

最佳答案

您可能缺少正确的依存关系,例如:

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


请注意,您的版本可能是2.5、3.0或3.1-取决于您使用的应用程序服务器。同样,当您要创建可执行文件war时,可能不应该使用提供的范围(取决于servlet容器)。

10-07 15:30