SpringBoot 学习记录(一): 简单web项目

最近学习SpringBoot,网上已经有非常好的博客,贴出来给大家分享,http://412887952-qq-com.iteye.com/category/356333?page=5

珠玉在前,这里主要是自己实践+深入记忆,便于后续回顾。

一,使用Spring提供的辅助工具:http://start.spring.io/,可以直接创建Maven工程

首先创建一个简单的web工程,输入依赖web,创建,然后导入eclipse。

SpringBoot 学习记录(一): 简单web项目_第1张图片

二,导入创建好的maven工程

1,目录结构

SpringBoot 学习记录(一): 简单web项目_第2张图片

2,pom.xml



	4.0.0

	com.example
	demo
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		1.3.6.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



3,新建测试HelloController
package com.example.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@RequestMapping("/sayHello")
	public String sayHello(String name){
		return "Hello: "+name;
	}
}
4,启动DemoApplication.java , 查看控制台打印启动信息
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.8.RELEASE)

2017-04-07 10:55:35.454  INFO 5580 --- [           main] com.example.DemoApplication              : Starting DemoApplication on PC-201611261236 with PID 5580 (H:\project\demo\target\classes started by Administrator in H:\project\demo)
2017-04-07 10:55:35.472  INFO 5580 --- [           main] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2017-04-07 10:55:35.584  INFO 5580 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@574b560f: startup date [Fri Apr 07 10:55:35 CST 2017]; root of context hierarchy
2017-04-07 10:55:37.686  INFO 5580 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8088 (http)
2017-04-07 10:55:37.705  INFO 5580 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-04-07 10:55:37.707  INFO 5580 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.37
2017-04-07 10:55:37.856  INFO 5580 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/spring-boot]   : Initializing Spring embedded WebApplicationContext
2017-04-07 10:55:37.856  INFO 5580 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2286 ms
2017-04-07 10:55:38.284  INFO 5580 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2017-04-07 10:55:38.290  INFO 5580 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-04-07 10:55:38.291  INFO 5580 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-04-07 10:55:38.291  INFO 5580 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-04-07 10:55:38.291  INFO 5580 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2017-04-07 10:55:38.688  INFO 5580 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@574b560f: startup date [Fri Apr 07 10:55:35 CST 2017]; root of context hierarchy
2017-04-07 10:55:38.803  INFO 5580 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/sayHello]}" onto public java.lang.String com.example.controller.HelloController.sayHello(java.lang.String)
2017-04-07 10:55:38.808  INFO 5580 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-04-07 10:55:38.809  INFO 5580 --- [           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-04-07 10:55:38.865  INFO 5580 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-07 10:55:38.865  INFO 5580 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-07 10:55:38.937  INFO 5580 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-04-07 10:55:39.178  INFO 5580 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-04-07 10:55:39.282  INFO 5580 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8088 (http)
2017-04-07 10:55:39.290  INFO 5580 --- [           main] com.example.DemoApplication              : Started DemoApplication in 4.305 seconds (JVM running for 5.016)
5,访问测试:http://localhost:8088/spring-boot/sayHello?name=World  =======  响应结果: Hello: World
6,请注意,默认tomcat端口号是8080,从根目录"/"启动,这里做了更改,

==== application.properties,spring_boot会默认读取此配置

server.port=8088
server.context-path=/spring-boot

一个简单的web项目就构建好了,接下来是加入数据库访问层,SpringBoot 学习记录(二): mybaits









你可能感兴趣的:(spring-boot)