gpt4 book ai didi

Spring boot + gradle + tomcat Hello World 应用程序不工作

转载 作者:行者123 更新时间:2023-11-28 22:29:40 25 4
gpt4 key购买 nike

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

可能是什么问题?

构建.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>

最佳答案

我在任何地方都没有看到 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);
}
}

关于Spring boot + gradle + tomcat Hello World 应用程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25707225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com