【问题标题】:Spring 4 with Annotation Tomcat Deployment 404 ErrorSpring 4 带有注释 Tomcat 部署 404 错误
【发布时间】:2015-12-31 03:20:55
【问题描述】:

我的项目使用 Spring 4、Servlet 3 API 和 Tomcat 8。我的部署有问题。我正在尝试在我的 VPS 中部署 WAR 包。我正在使用 Intellij IDEA。

我将 WAR 复制到 /opt/tomcat/webapps 路径。我可以从 Tomcat 的应用程序页面看到 WAR。但是当我尝试浏览 URL 时,我得到 404 Not Found 错误。

我正在使用带有注释和空 web.xml 的 Spring,并且我正在使用 Spring Security。

WepAppInitializer.java

public class WepAppInitializer implements WebApplicationInitializer {

@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(WebConfig.class);
    ctx.setServletContext(servletContext);
    ServletRegistration.Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    dynamic.addMapping("/acentecilik");
    dynamic.setLoadOnStartup(1);
    }
}

WebConfig.java

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan("com.decimatech.acentecilik")
@PropertySource("classpath:application.properties")
public class WebConfig extends WebMvcConfigurerAdapter{

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new ThymeleafLayoutInterceptor());
}


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
    registry.addResourceHandler("/resources/**").addResourceLocations("/static/");
}

@Bean
@Description("Thymeleaf template resolver serving HTML 5")
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
    templateResolver.setPrefix("/WEB-INF/html/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("LEGACYHTML5");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);

    return templateResolver;
}

@Bean
@Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());

    return templateEngine;
}

@Bean
@Description("Thymeleaf view resolver")
public ThymeleafViewResolver viewResolver() {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    viewResolver.setContentType("text/html;charset=UTF-8");
    viewResolver.setCharacterEncoding("utf-8");

    return viewResolver;
}

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Value("${spring.datasource.url}")
private String datasourceUrl;

@Value("${spring.datasource.username}")
private String datasourceUsername;

@Value("${spring.datasource.password}")
private String datasourcePassword;

@Bean(name = "dataSource")
public DataSource getDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driverClassName);
    dataSource.setUrl(datasourceUrl);
    dataSource.setUsername(datasourceUsername);
    dataSource.setPassword(datasourcePassword);

    return dataSource;
}

@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

    return transactionManager;
}

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {

    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

    sessionBuilder.scanPackages("com.decimatech.acentecilik.model");
    sessionBuilder.addProperties(getHibernateProperties());

    return sessionBuilder.buildSessionFactory();
}

private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
    properties.put("hibernate.hbm2ddl.auto", "update");
    properties.put("hibernate.show_sql", "true");
    properties.put("hibernate.format_sql", "true");
    properties.put("hibernate.use_sql_comments", "true");
    properties.put("hibernate.enable_lazy_load_no_trans", "true");
    return properties;
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1"
     metadata-complete="true">

<display-name>Acentecilik</display-name>
<description>
    Acentecilik
</description>
</web-app>

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.decimatech</groupId>
<artifactId>acentecilik</artifactId>
<version>1.0-SNAPSHOT</version>

//Some package dependencies

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

这是我使用的设置的一些截图。

IDEA WAR 设置

项目结构

Tomcat 管理应用页面

还有 404 错误

我该如何解决这个问题?

这是我的 catalina 日志文件的输出。我将 WAR 的所有权从 root 更改为 tomcat 用户。但还是同样的问题。

2015 年 10 月 2 日 16:03:23.800 严重 [localhost-startStop-10] org.apache.catalina.startup.ContextConfig.beforeStart 为上下文修复 docBase 异常 [/acentecilik] java.io.IOException:无法创建目录 [/opt/tomcat/webapps/acentecilik] 在 org.apache.catalina.startup.ExpandWar.expand(ExpandWar.java:115) 在 org.apache.catalina.startup.ContextConfig.fixDocBase(ContextConfig.java:618) 在 org.apache.catalina.startup.ContextConfig.beforeStart(ContextConfig.java:744) 在 org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:307) 在 org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95) 在 org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) 在 org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:402) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147) 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717) 在 org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:945) 在 org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1798) 在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 在 java.util.concurrent.FutureTask.run(FutureTask.java:262) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 在 java.lang.Thread.run(Thread.java:745)

2015 年 10 月 2 日 16:03:58.561 INFO [localhost-startStop-10] org.apache.jasper.servlet.TldScanner.scanJars 至少对一个 JAR 进行了 TLD 扫描,但未包含 TLD。启用此记录器的调试日志记录以获取已扫描但未在其中找到 TLD 的 JAR 的完整列表。在扫描期间跳过不需要的 JAR 可以缩短启动时间和 JSP 编译时间。

2015 年 10 月 2 日 16:03:58.649 INFO [localhost-startStop-10] org.apache.catalina.startup.HostConfig.deployWAR 部署 Web 应用程序存档 /opt/tomcat/webapps/acentecilik.war 已完成34,860 毫秒

编辑 我将 /opt/tomcat/webapps 所有权更改为 tomcat,现在我没有收到 404 错误。但现在我有了这个。

INFO [http-nio-8080-exec-17] org.apache.catalina.core.ApplicationContext.log 在类路径上未检测到 Spring WebApplicationInitializer 类型

【问题讨论】:

  • 你检查过Tomcat日志吗?我想没有什么,因为你得到一个 404 错误但只是为了确定..
  • 只有一件事,题外话,你试图隐藏整个代码中提到的“localhost”这个词?
  • 没有。这是我的测试服务器的IP。所以现实世界的ip。 @ChaibiAlaa
  • 我很想投票关闭它,因为“寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及最短的代码有必要在问题本身中重现它。没有明确问题陈述的问题对其他读者没有用处。一旦你解决了一个问题,可能还有很多其他问题。 @destan 已经回答了第一个问题。您应该使用最小的示例来重现问题、部署问题,而不是像这里那样复杂的配置。
  • 我写了我所有的配置文件,因为我不知道问题出在哪里。我认为这个问题可能与配置或 Tomcat 或 Maven 有关。这就是为什么我写了这么多关于问题的文章。

标签: java spring maven tomcat intellij-idea


【解决方案1】:

异常说

org.apache.catalina.startup.ContextConfig.beforeStart 异常修复 docBase for context [/acentecilik] java.io.IOException: 无法创建目录

你已经说过tomcat在/opt之下。所以我想tomcat没有足够的权限在自己的目录/opt/tomcat上创建(写权限)。

如果您向启动 tomcat 的用户(很可能是您自己的用户)授予写入权限,那么问题就会消失。

sudo chmod -R 0744 /opt/tomcat

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 2019-02-27
    • 2017-07-07
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    相关资源
    最近更新 更多