在IntelliJ 15中运行Spring Boot MVC时路由404

时间:2016-01-12 17:20:39

标签: java spring spring-mvc intellij-idea

我正在尝试从IntelliJ 15.0.2中的Mastering Spring MVC 4运行一个非常简单的示例。我可以从命令行使用./gradlew bootRun正确运行示例,但不能从IntelliJ。

当我使用./gradlew bootRun运行应用程序时,我在http://localhost:8080处看到了模板。当我在IntelliJ中通过Run运行应用程序时,应用程序似乎正常启动,但我收到了白色标签404页。

应用

package masterSpringMvc;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MasterSpringMvcApplication {

    public static void main(String[] args) {
        SpringApplication.run(MasterSpringMvcApplication.class, args);
    }
}

src/main/java/masterSpringMvc/controller

中的控制器
package masterSpringMvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {

    @RequestMapping("/")
    public String hello() {
        return "resultPage";
    }
}

src/main/resources/templates

中的模板
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta charset="UTF-8"/>
    <title>Hello thymeleaf</title>
</head>
<body>
<span th:text="|Hello thymeleaf|">Hello html</span>
</body>
</html>

的build.gradle

buildscript {
    ext {
        springBootVersion = '1.3.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot' 

jar {
    baseName = 'masterSpringMvc'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.9'
}

./ gradlew bootRun

% ./gradlew bootRun                                                                                                                                       
:compileJava UP-TO-DATE
:processResources
:classes
:findMainClass
:bootRun

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.1.RELEASE)

2016-01-12 10:18:36.263  INFO 8812 --- [           main] m.MasterSpringMvcApplication             : Starting MasterSpringMvcApplication on duffn with PID 8812 (/Users/nickduffy/Dropbox/Development/learning/java/MasterMvc/build/classes/main started by nickduffy in /Users/nickduffy/Dropbox/Development/learning/java/MasterMvc)
2016-01-12 10:18:36.266  INFO 8812 --- [           main] m.MasterSpringMvcApplication             : No active profile set, falling back to default profiles: default
2016-01-12 10:18:36.503  INFO 8812 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5204062d: startup date [Tue Jan 12 10:18:36 MST 2016]; root of context hierarchy
2016-01-12 10:18:36.942  INFO 8812 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-12 10:18:37.493  INFO 8812 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-12 10:18:37.503  INFO 8812 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-12 10:18:37.504  INFO 8812 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-12 10:18:37.574  INFO 8812 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-12 10:18:37.574  INFO 8812 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1074 ms
2016-01-12 10:18:37.797  INFO 8812 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-12 10:18:37.801  INFO 8812 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-12 10:18:37.802  INFO 8812 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-12 10:18:37.802  INFO 8812 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-12 10:18:37.802  INFO 8812 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-12 10:18:38.046  INFO 8812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5204062d: startup date [Tue Jan 12 10:18:36 MST 2016]; root of context hierarchy
2016-01-12 10:18:38.112  INFO 8812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String masterSpringMvc.controller.HelloController.hello()
2016-01-12 10:18:38.115  INFO 8812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-01-12 10:18:38.115  INFO 8812 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-01-12 10:18:38.137  INFO 8812 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 10:18:38.137  INFO 8812 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 10:18:38.164  INFO 8812 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 10:18:38.553  INFO 8812 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-12 10:18:38.625  INFO 8812 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-12 10:18:38.629  INFO 8812 --- [           main] m.MasterSpringMvcApplication             : Started MasterSpringMvcApplication in 2.608 seconds (JVM running for 2.907)
> Building 80% > :bootRun

bootRun请求成功

2016-01-12 10:29:30.798  INFO 9103 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-12 10:29:30.803  INFO 9103 --- [           main] m.MasterSpringMvcApplication             : Started MasterSpringMvcApplication in 2.753 seconds (JVM running for 3.068)
2016-01-12 10:29:36.617  INFO 9103 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-12 10:29:36.617  INFO 9103 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-12 10:29:36.628  INFO 9103 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 11 ms
2016-01-12 10:29:36.644 DEBUG 9103 --- [nio-8080-exec-1] o.s.b.c.web.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@751fc5a9
2016-01-12 10:29:36.671 DEBUG 9103 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF] INITIALIZING TEMPLATE ENGINE
2016-01-12 10:29:36.709 DEBUG 9103 --- [nio-8080-exec-1] o.t.t.AbstractTemplateResolver           : [THYMELEAF] INITIALIZING TEMPLATE RESOLVER: org.thymeleaf.templateresolver.TemplateResolver
2016-01-12 10:29:36.710 DEBUG 9103 --- [nio-8080-exec-1] o.t.t.AbstractTemplateResolver           : [THYMELEAF] TEMPLATE RESOLVER INITIALIZED OK
2016-01-12 10:29:36.710 DEBUG 9103 --- [nio-8080-exec-1] o.t.m.AbstractMessageResolver            : [THYMELEAF] INITIALIZING MESSAGE RESOLVER: org.thymeleaf.spring4.messageresolver.SpringMessageResolver
2016-01-12 10:29:36.710 DEBUG 9103 --- [nio-8080-exec-1] o.t.m.AbstractMessageResolver            : [THYMELEAF] MESSAGE RESOLVER INITIALIZED OK
2016-01-12 10:29:36.714 DEBUG 9103 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine.CONFIG      : [THYMELEAF] TEMPLATE ENGINE CONFIGURATION:
[THYMELEAF] * Cache Factory implementation: org.thymeleaf.cache.StandardCacheManager
[THYMELEAF] * Template modes:
[THYMELEAF]     * HTML5
[THYMELEAF]     * VALIDXHTML
[THYMELEAF]     * LEGACYHTML5
[THYMELEAF]     * XML
[THYMELEAF]     * XHTML
[THYMELEAF]     * VALIDXML
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF]     * org.thymeleaf.templateresolver.TemplateResolver
[THYMELEAF] * Message resolvers (in order):
[THYMELEAF]     * org.thymeleaf.spring4.messageresolver.SpringMessageResolver
[THYMELEAF] * Dialect [1 of 2]: org.thymeleaf.spring4.dialect.SpringStandardDialect
[THYMELEAF]     * Prefix: "th"
[THYMELEAF] * Dialect [2 of 2]: nz.net.ultraq.thymeleaf.LayoutDialect
[THYMELEAF]     * Prefix: "layout"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
2016-01-12 10:29:36.714 DEBUG 9103 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF] TEMPLATE ENGINE INITIALIZED
2016-01-12 10:29:36.904 DEBUG 9103 --- [nio-8080-exec-1] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@751fc5a9
2016-01-12 10:29:37.448 DEBUG 9103 --- [nio-8080-exec-2] o.s.b.c.web.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@751fc5a9
2016-01-12 10:29:37.459 DEBUG 9103 --- [nio-8080-exec-2] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@751fc5a9
> 2016-01-12 10:33:50.354  INFO 9103 --- [       Thread-2] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@376b4233: startup date [Tue Jan 12 10:29:28 MST 2016]; root of context hierarchy

IntelliJ Run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.1.RELEASE)

2016-01-12 10:19:32.964  INFO 8837 --- [           main] m.MasterSpringMvcApplication             : Starting MasterSpringMvcApplication on duffn with PID 8837 (/Users/nickduffy/Dropbox/Development/learning/java/MasterMvc/build/classes/main started by nickduffy in /Users/nickduffy/Dropbox/Development/learning/java/MasterMvc)
2016-01-12 10:19:32.967  INFO 8837 --- [           main] m.MasterSpringMvcApplication             : No active profile set, falling back to default profiles: default
2016-01-12 10:19:33.016  INFO 8837 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@587c290d: startup date [Tue Jan 12 10:19:33 MST 2016]; root of context hierarchy
2016-01-12 10:19:33.904  INFO 8837 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2016-01-12 10:19:34.431  INFO 8837 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-01-12 10:19:34.442  INFO 8837 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-01-12 10:19:34.442  INFO 8837 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.30
2016-01-12 10:19:34.506  INFO 8837 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-01-12 10:19:34.506  INFO 8837 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1494 ms
2016-01-12 10:19:34.718  INFO 8837 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2016-01-12 10:19:34.721  INFO 8837 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2016-01-12 10:19:34.721  INFO 8837 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-01-12 10:19:34.721  INFO 8837 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2016-01-12 10:19:34.721  INFO 8837 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2016-01-12 10:19:34.892  INFO 8837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@587c290d: startup date [Tue Jan 12 10:19:33 MST 2016]; root of context hierarchy
2016-01-12 10:19:34.936  INFO 8837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String masterSpringMvc.controller.HelloController.hello()
2016-01-12 10:19:34.939  INFO 8837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-01-12 10:19:34.940  INFO 8837 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-01-12 10:19:34.959  INFO 8837 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 10:19:34.959  INFO 8837 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 10:19:34.985  INFO 8837 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-01-12 10:19:35.059  INFO 8837 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-01-12 10:19:35.113  INFO 8837 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-12 10:19:35.117  INFO 8837 --- [           main] m.MasterSpringMvcApplication             : Started MasterSpringMvcApplication in 2.446 seconds (JVM running for 2.858)

Intellis请求404s

2016-01-12 10:33:56.916  INFO 9305 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2016-01-12 10:33:56.920  INFO 9305 --- [           main] m.MasterSpringMvcApplication             : Started MasterSpringMvcApplication in 2.494 seconds (JVM running for 2.842)
2016-01-12 10:34:04.328  INFO 9305 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-01-12 10:34:04.328  INFO 9305 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2016-01-12 10:34:04.337  INFO 9305 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 9 ms
2016-01-12 10:34:04.348 DEBUG 9305 --- [nio-8080-exec-1] o.s.b.c.web.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@7a29e417
2016-01-12 10:34:04.370 DEBUG 9305 --- [nio-8080-exec-1] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@7a29e417
2016-01-12 10:34:04.880 DEBUG 9305 --- [nio-8080-exec-2] o.s.b.c.web.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@7a29e417
2016-01-12 10:34:04.886 DEBUG 9305 --- [nio-8080-exec-2] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@7a29e417

我的Run配置中唯一的项目是Main class:masterSpringMvc.MasterSpringMvcApplication。

我还需要在IntelliJ中添加哪些配置才能成功启动应用程序?

1 个答案:

答案 0 :(得分:1)

看起来IDEA项目尚未更新某些依赖项,Spring Boot自动配置没有从类路径中获取所有依赖项。

尝试重新导入您的库并重建项目。请参阅IDEA文档,了解如何使用Gradle执行此操作。