Spring 4 with Annotation Tomcat Deployment 404 Error

时间:2015-10-02 20:18:31

标签: java spring maven tomcat intellij-idea

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

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

我使用Spring注释并清空web.xml,我也在使用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设置 IDEA WAR Settings

项目结构

Project Structures

Tomcat管理应用页面

Tomcat Manage Apps Page

和404错误 404 Error

我该如何解决这个问题?

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

  

02-Oct-2015 16:03:23.800 SEVERE [localhost-startStop-10] org.apache.catalina.startup.ContextConfig.beforeStart异常修复docBase for context [/ 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)           at 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)           at 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)           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)           在java.lang.Thread.run(Thread.java:745)

     

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

     

02-Oct-2015 16:03:58.649 INFO [localhost-startStop-10] org.apache.catalina.startup.HostConfig.deployWAR Web应用程序归档文件/opt/tomcat/webapps/acentecilik.war的部署已完成34,860 ms

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

  

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

1 个答案:

答案 0 :(得分:3)

例外说

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

你已经说过tomcat在/opt/tomcat之下了。所以我认为tomcat没有足够的权限在自己的目录 sudo chmod -R 0744 /opt/tomcat 上创建(写权限)。

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

requests.post(url, data=xml_string, headers={'Content-Type':'application/xml; charset=UTF-8'})