IntelliJ IDEA Spring Boot-----Hello例子

南陈 2017-07-11 07:20:50
由于工作需要,今天我看了Spring Cloud微服务。根据学习文档,了解一下Spring Boot微服务构建,于是想一个hello案例,结果得不到想要的结果(打印hello world):报404错误。
安装最新版本Intellij IDEA,然后根据网上的资料配置环境,配置Tomcat之类的。接着根据开发文档,访问http://s七art. spring. io/ 该页面提供了以Maven 或Gradle构建Spring Boot项目的功能。然后将项目导进IDEA工具中。最后在运行项目的时候出现问题,自己也不是款很清楚应该怎么做,希望各位大神能给点意见或者资料看看。

由于对IDEA开发工具不是很了解,有些问题描述的不是很清楚,请大家见谅!

项目代码如下:







项目运行:


2017-07-11 19:03:54.609 INFO 21892 --- [ main] com.example.hello.HelloApplication : Starting HelloApplication on DESKTOP-KBBHDKV with PID 21892 (D:\WSWorkspace\hello\target\classes started by cxstar in D:\WSWorkspace\hello)
2017-07-11 19:03:54.613 INFO 21892 --- [ main] com.example.hello.HelloApplication : No active profile set, falling back to default profiles: default
2017-07-11 19:03:54.684 INFO 21892 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@646db9: startup date [Tue Jul 11 19:03:54 CST 2017]; root of context hierarchy
2017-07-11 19:03:56.367 INFO 21892 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-11 19:03:56.380 INFO 21892 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-07-11 19:03:56.381 INFO 21892 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.15
2017-07-11 19:03:56.469 INFO 21892 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-11 19:03:56.470 INFO 21892 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1789 ms
2017-07-11 19:03:56.613 INFO 21892 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-11 19:03:56.619 INFO 21892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-11 19:03:56.620 INFO 21892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-11 19:03:56.620 INFO 21892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-11 19:03:56.620 INFO 21892 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-11 19:03:56.964 INFO 21892 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@646db9: startup date [Tue Jul 11 19:03:54 CST 2017]; root of context hierarchy
2017-07-11 19:03:57.025 INFO 21892 --- [ 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)
2017-07-11 19:03:57.026 INFO 21892 --- [ 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)
2017-07-11 19:03:57.061 INFO 21892 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-11 19:03:57.061 INFO 21892 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-11 19:03:57.106 INFO 21892 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-11 19:03:57.247 INFO 21892 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-11 19:03:57.308 INFO 21892 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-11 19:03:57.314 INFO 21892 --- [ main] com.example.hello.HelloApplication : Started HelloApplication in 3.084 seconds (JVM running for 3.606)


...全文
1073 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Alina_FZGong 2018-03-22
  • 打赏
  • 举报
回复
/** * Created by Rhett on 18/3/22. */ @EnableAutoConfiguration @RestController public class HelloController { @RequestMapping("hello") String index(){ return "Hello Word!"; } } 加上@EnableAutoConfiguration注解就可以了
AYellow2048 2017-07-12
  • 打赏
  • 举报
回复
整这么麻烦,直接用它的例子不就行了?一个类,右键直接运行 package hello; import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller @EnableAutoConfiguration public class SampleController { @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(SampleController.class, args); } } SpringBootApplication等于是@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解的结合,默认扫描当前包的子包,如果像你那样,把web相关的类弄在另一个包里,就需要设置scanBasePackages属性。
南陈 2017-07-12
  • 打赏
  • 举报
回复
运行结果是不是报错了? 2017-07-12 10:24:31.018 INFO 7584 --- [ 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) 2017-07-12 10:24:31.019 INFO 7584 --- [ 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)
南陈 2017-07-12
  • 打赏
  • 举报
回复
我改成这样了 @RestController @EnableAutoConfiguration public class HelloApplication { @RequestMapping("/") public String index(){ return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } }
rusth2010 2017-07-12
  • 打赏
  • 举报
回复
跟我来。在helloworld 最下面写个main方法
/**
* 运行主方法
*
* @param args
*/
public static void main(String[] args) {
//如果以此方式启动 请配置@SpringBootApplication
SpringApplication.run(HelloWorld.class, args);
}
rusth2010 2017-07-12
  • 打赏
  • 举报
回复
不需要部署到tomcat里面的。这两天刚好我也在看。helloworld的开始很简单的。
南陈 2017-07-12
  • 打赏
  • 举报
回复
楼上的两种方法都是过了,还是不行
南陈 2017-07-12
  • 打赏
  • 举报
回复
问题解决了,谢谢大家
AYellow2048 2017-07-11
  • 打赏
  • 举报
回复
@SpringBootApplication(scanBasePackages = {"com.example.web"})
我爱娃哈哈 2017-07-11
  • 打赏
  • 举报
回复
springboot项目不需要配置tomcat的 你直接在springBoot那里面启动HelloApplication就行了

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧