SpringBoot學習(四)-->SpringBoot快速入門,開山篇
阿新 • • 發佈:2018-07-02
hand return all mapped min embedded ica 轉發 gis
SpringBoot是伴隨著Spring4.0誕生的,旨在簡化開發。
SpringBoot官方文檔:http://spring.io/projects/spring-boot
寫個示例:Hello SpringBoot
1、創建Maven工程
工程結構如下:
2、配置pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mmzs</groupId> <artifactId>springBoot00</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>springBoot00 Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <!-- 一定要有spring-boot-starter-parent,其中包含了spring的各種插件版本號 --> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /><!-- lookup parent from repository --> </parent> <!-- 父類統一管理版本信息 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- springboot 默認java版本是1.6,這裏顯示給它指定為1.7 --> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <!-- 導入spring boot的web支持,可以不寫版本號,在spring-boot-starter-parent已經包含 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <finalName>spring_boot</finalName> <!-- 添加Spring boot的maven插件,可以不寫版本號,在spring-boot-starter-parent已經包含 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </build> </project>
3、編寫代碼
1 package com.mmzs.springboot; 2 3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestParam; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 import org.springframework.web.bind.annotation.RestController; 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 /** 14 * Created by mmzs 2018年4月2日 11:50:57 15 * springboot註解詳解:http://www.cnblogs.com/mmzs/p/8874349.html 16 */ 17 //用於標註控制層組件(如struts中的action),@ResponseBody和@Controller的合集, 18 //這樣子獲取的數據返回前臺時也會自動轉發為json格式。 19 @RestController 20 //Spring Boot自動配置(auto-configuration):嘗試根據你添加的jar依賴自動配置你的Spring應用。 21 @EnableAutoConfiguration 22 public class HelloController { 23 24 @RequestMapping("/hello") 25 @ResponseBody //會使用詳細轉換器輸出結果 26 public String hello() { 27 return "Hello Spring-Boot"; 28 } 29 30 @RequestMapping("/info") 31 public Map<String, String> getInfo(@RequestParam String name) { 32 Map<String, String> map = new HashMap<>(); 33 map.put("name", name); 34 return map; 35 } 36 37 @RequestMapping("/list") 38 public List<Map<String, String>> getList() { 39 List<Map<String, String>> list = new ArrayList<>(); 40 Map<String, String> map = null; 41 for (int i = 1; i <= 5; i++) { 42 map = new HashMap<>(); 43 map.put("name", "mmzs+" + i); 44 list.add(map); 45 } 46 return list; 47 } 48 } 49HelloController
1 package com.mmzs.springboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 /** 7 * Created by mmzs 2018年4月2日 11:48:38 8 * springboot註解詳解:http://www.cnblogs.com/mmzs/p/8874349.html 9 */ 10 //Spring Boot項目的核心註解,主要目的是開啟自動配置。 11 //包含了@ComponentScan、@Configuration和@EnableAutoConfiguration註解。 12 //其中@ComponentScan默認掃描@SpringBootApplication所在類的同級目錄以及它的子目錄。 13 @SpringBootApplication 14 public class Application { 15 public static void main(String[] args) { 16 SpringApplication.run(Application.class, args); 17 } 18 } 19Application啟動類
4、測試效果
運行Application類,結果如下:
. ____ _ __ _ _ /\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.2.RELEASE) 2018-07-02 12:17:03.434 INFO 5988 --- [ main] com.mmzs.springboot.Application : Starting Application on CVHOPASEHFXPHFV with PID 5988 (D:\Project\springBoot00\target\classes started by Administrator in D:\Project\springBoot00) 2018-07-02 12:17:03.442 INFO 5988 --- [ main] com.mmzs.springboot.Application : No active profile set, falling back to default profiles: default 2018-07-02 12:17:03.562 INFO 5988 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27fe3806: startup date [Mon Jul 02 12:17:03 CST 2018]; root of context hierarchy 2018-07-02 12:17:06.724 INFO 5988 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-07-02 12:17:06.751 INFO 5988 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2018-07-02 12:17:06.753 INFO 5988 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11 2018-07-02 12:17:06.971 INFO 5988 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-07-02 12:17:06.972 INFO 5988 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3415 ms 2018-07-02 12:17:07.260 INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: ‘dispatcherServlet‘ to [/] 2018-07-02 12:17:07.266 INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘characterEncodingFilter‘ to: [/*] 2018-07-02 12:17:07.267 INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*] 2018-07-02 12:17:07.267 INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘httpPutFormContentFilter‘ to: [/*] 2018-07-02 12:17:07.267 INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: ‘requestContextFilter‘ to: [/*] 2018-07-02 12:17:07.788 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27fe3806: startup date [Mon Jul 02 12:17:03 CST 2018]; root of context hierarchy 2018-07-02 12:17:07.928 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/list]}" onto public java.util.List<java.util.Map<java.lang.String, java.lang.String>> com.mmzs.springboot.HelloController.getList() 2018-07-02 12:17:07.929 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.mmzs.springboot.HelloController.hello() 2018-07-02 12:17:07.931 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/info]}" onto public java.util.Map<java.lang.String, java.lang.String> com.mmzs.springboot.HelloController.getInfo(java.lang.String) 2018-07-02 12:17:07.936 INFO 5988 --- [ 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) 2018-07-02 12:17:07.937 INFO 5988 --- [ 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) 2018-07-02 12:17:08.012 INFO 5988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-07-02 12:17:08.013 INFO 5988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-07-02 12:17:08.103 INFO 5988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-07-02 12:17:08.403 INFO 5988 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-07-02 12:17:08.531 INFO 5988 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-07-02 12:17:08.538 INFO 5988 --- [ main] com.mmzs.springboot.Application : Started Application in 5.736 seconds (JVM running for 6.592)啟動成功界面
訪問界面:
SpringBoot學習(四)-->SpringBoot快速入門,開山篇