1、使用Eclipse 建立Maven項目(webapp OR quickstart) 2、配置Maven,如下: 3、建立啟動Application 4、編輯Controller 5、通過application.properties對項目進行配置 項目文件佈局如下: 啟動Application程式 ...
1、使用Eclipse 建立Maven項目(webapp OR quickstart)
2、配置Maven,如下:
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>1.2.5.RELEASE</version> 5 <relativePath/> 6 </parent> 7 8 <properties> 9 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 10 <java.version>1.8</java.version> 11 </properties> 12 13 <dependencies> 14 <dependency> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-web</artifactId> 17 </dependency> 18 </dependencies> 19 20 <build> 21 <plugins> 22 <plugin> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-maven-plugin</artifactId> 25 </plugin> 26 </plugins> 27 </build>
3、建立啟動Application
1 package demo.web.application; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 import org.springframework.context.annotation.ComponentScan; 5 6 @SpringBootApplication 7 @ComponentScan(basePackages={"demo.web.*"}) 8 public class Application { 9 public static void main(String[] args) { 10 SpringApplication.run(Application.class, args); 11 } 12 13 }
4、編輯Controller
1 package demo.web.controller; 2 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 3 import org.springframework.web.bind.annotation.PathVariable; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RestController; 6 7 @RestController 8 @EnableAutoConfiguration 9 public class HelloController { 10 11 @RequestMapping("/") 12 String home() { 13 System.out.println("ee"); 14 return "Hello World!"; 15 } 16 17 @RequestMapping("/hello/{myName}") 18 String index(@PathVariable String myName) { 19 return "Hello "+myName+"!!!"; 20 } 21 22 }
5、通過application.properties對項目進行配置
server.port=9000
項目文件佈局如下:
啟動Application程式,即可訪問網站。