java - 无法在intellij中将Spring mvc部署到tomcat

标签 java spring maven spring-mvc

我正在尝试将我的简单的spring MVC项目(没有spring boot)部署到IntelliJ中的tomcat,但是我无法让它工作,服务器总是返回404。我已经尝试了很多教程,但它仍然不起作用。

我也尝试过 AbstractAnnotationConfigDispatcherServletInitializer 类,但它也不起作用。似乎永远不会调用 onStartup 方法。

有人可以指出我错在哪里并使其发挥作用吗?

这是我的 pom.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.learning</groupId>
    <artifactId>spring-web-no-boot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <org.springframework.version>5.1.5.RELEASE</org.springframework.version>
    </properties>

    <dependencies>
        <!-- core spring dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
            <!--<scope>runtime</scope>-->
        </dependency>
        <!-- spring web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>

        <!-- provide servlet, web server -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>9.0.16</version>
        </dependency>

        <!-- json jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
            <version>2.9.8</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.9.8</version>
        </dependency>
    </dependencies>
</project>

Application.java 文件

import configuration.RootConfig;
import configuration.WebConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class Application implements WebApplicationInitializer {
    public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(RootConfig.class);
        servletContext.addListener(new ContextLoaderListener(rootContext));

        AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
        webContext.register(WebConfig.class);
        DispatcherServlet servlet = new DispatcherServlet(webContext);
        ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }
}

RootConfig.java 文件

package configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "controller")
public class RootConfig {
}

WebConfig.java 文件

package configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@Configuration
public class WebConfig {
}

HomeController.java 文件

package controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home() {
        return "Hello world from spring";
    }
}

最佳答案

首先你应该添加<packaging>war</packaging>给您pom.xml ,但这不会导致错误。

pom.xml

<groupId>com.learning</groupId>
<artifactId>spring-web-no-boot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

您正在使用两个不同的上下文。如果您仅使用根上下文,它应该可以工作。

尝试以下Application.java:

public class Application implements WebApplicationInitializer {
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context =
                new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("configuration");
        container.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher =
                container.addServlet("dispatcher", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

关于java - 无法在intellij中将Spring mvc部署到tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54741100/

相关文章:

spring - URL参数如何映射到Spring Boot Controller 上的对象?

java - Jenkins Android 库 - 不支持的 Major.minor 版本 51.0

java - Android 根文件夹读写权限

java - Java EE 服务器上的 Exec 运行时

java - 使用java swing更新sql上的数据

java - 在 Maven 测试上生成数据库架构(Hibernate)

java - Maven pom.xml 本地依赖关系未解决

java - 平移后更新android中的图像坐标

java - 组织.hibernate.PropertyAccessException : Could not set field value with Composite Key

spring - 如何在 Spring MVC 中模拟安全上下文以进行测试