我正在尝试使用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' }
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");
}
}
}
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;
}
}
的hello.jsp
<html>
<head>
<title>Hello world page</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
答案 0 :(得分:0)
我没有在任何地方看到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);
}
}
答案 1 :(得分:0)
如果您尝试在tomcat上运行它,那么为什么不将它打包到jar文件中并使用嵌入式tomcat?当然,您必须稍微更改一个项目结构。