【问题标题】:Spring boot + gradle + tomcat hello world app not workingSpring boot + gradle + tomcat hello world 应用程序不起作用
【发布时间】:2014-10-31 16:00:55
【问题描述】:

我正在尝试使用 spring boot、gradle 和 tomcat 实现一个基本的 hello world web 应用程序。我有以下java类。当我执行“./gradlew clean tomcatRunWar”时,tomcat刚刚启动但无法访问。

可能是什么问题?

build.gradle

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath 'org.gradle.api.plugins:gradle-tomcat-plugin:0.9.8'
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.1.6.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
apply plugin: 'tomcat'

mainClassName = "au.com.aeas.config.WebAppInitializer"

war {
    baseName = ''
    version =  '0.0.0'
}

repositories {
    mavenLocal()
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    tomcat "org.apache.tomcat.embed:tomcat-embed-core:7.0.42",
        "org.apache.tomcat.embed:tomcat-embed-logging-juli:7.0.42"
    tomcat("org.apache.tomcat.embed:tomcat-embed-jasper:7.0.42") {
        exclude group: 'org.eclipse.jdt.core.compiler', module: 'ecj'
    }

    compile 'org.springframework.security:spring-security-config:3.2.5.RELEASE'
    compile 'org.springframework.security:spring-security-web:3.2.5.RELEASE'
    compile("org.springframework.boot:spring-boot-starter-web:1.1.6.RELEASE")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat:1.1.6.RELEASE")
}

task wrapper(type: Wrapper) { gradleVersion = '1.6' }
  1. WebAppInitializer.java

      @EnableAutoConfiguration
      @ComponentScan("au.com.aeas.web.controller")
      @Controller
      public class WebAppInitializer extends WebMvcConfigurerAdapter {
    
      public static void main(String[] args) throws Exception {
         new SpringApplicationBuilder(WebAppInitializer.class).run(args);
      }
    
      @Bean
      public ApplicationSecurity applicationSecurity() {
         return new ApplicationSecurity();
      }
    
      @Bean
      public InternalResourceViewResolver viewResolver() {
         InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
         internalResourceViewResolver.setPrefix("WEB-INF/views/");
         internalResourceViewResolver.setSuffix(".jsp");
         return internalResourceViewResolver;
      }
    
      @Bean
      public AuthenticationSecurity authenticationSecurity() {
         return new AuthenticationSecurity();
      }
    
      @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
      protected static class ApplicationSecurity extends
        WebSecurityConfigurerAdapter {
    
        @Autowired
        private SecurityProperties security;
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
             http.authorizeRequests().anyRequest().fullyAuthenticated().and()
                .formLogin();
        }
      }
    
      @Order(Ordered.HIGHEST_PRECEDENCE + 10)
      protected static class AuthenticationSecurity extends
        GlobalAuthenticationConfigurerAdapter {
    
      @Override
      public void init(AuthenticationManagerBuilder auth) throws Exception {
           auth.inMemoryAuthentication().withUser("user").password("password")
                .roles("USER");
       }
      }
     }
    
  2. HelloController.java

    @Controller
    public class HelloController {
    
      @RequestMapping(value = "/hello", method = RequestMethod.GET)
      public ModelAndView hello() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("message", "charith");
        modelAndView.setViewName("hello");
        return modelAndView;
      }
    
    }
    
  3. 你好.jsp

    <html>
    <head>
      <title>Hello world page</title>
    </head>
    <body>
       <h1>${message}</h1>
    </body>
    </html> 
    

【问题讨论】:

  • 控制台有异常吗?您尝试访问时收到的错误消息是什么?

标签: spring tomcat gradle spring-boot


【解决方案1】:

我在任何地方都看不到ServletContextInitializer,所以我猜gradle bootRun(或运行您的主要方法)会起作用,但部署到Tomcat 不会。一个基本的ServletContextInitializer 很容易编写并且有据可查(例如这里:https://spring.io/guides/gs/convert-jar-to-war-maven/)。示例:

public class WebInitializer extends SpringBootServletInitializer {
  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(WebAppIninitializer.class);
  }
}

【讨论】:

    【解决方案2】:

    如果你想在 tomcat 上运行它,那么为什么不将它打包成 jar 文件并使用嵌入式 tomcat 呢?当然,您将不得不稍微更改项目结构。

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 2017-12-04
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-16
      相关资源
      最近更新 更多