Spring Boot入門的第一個例子,最簡單的Web項目。 ...
1. 新建一個Maven Web項目。
2. 配置pom.xml文件。
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yws710.springboot</groupId> <artifactId>demo1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
3. 編寫控制器類
package com.yws710.springboot.demo1.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by Administrator on 2017/7/19. */ @Controller public class HelloController { @ResponseBody @RequestMapping("/hello") public String hello() { return "Hello, Spring Boot!"; } }
4. 編寫啟動類
package com.yws710.springboot.demo1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by Administrator on 2017/7/19. */ @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5. 啟動項目。只需要運行上面代碼的main方法,運行成功,控制台輸出如下:
"D:\Program Files\Java\jdk1.7.0_67\bin\java" ... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.4.RELEASE) 省略部分信息 2017-07-20 00:16:44.849 INFO 5388 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-07-20 00:16:44.858 INFO 5388 --- [ main] com.yws710.springboot.demo1.App : Started App in 5.835 seconds (JVM running for 6.364)
6. 在瀏覽器中輸入 http://localhost:8080/hello,顯示結果如下:
好了,一個最簡單的Web項目完成了。沒有寫任何的配置文件,也沒有任何的xml文件(這裡完全可以把web.xml文件刪掉)。