springboot內嵌tomcat的helloworld
springboot已經整合了tomcat,寫一個restful的helloworld很簡單,在pom.xml里的dependencies標簽下添加spring-boot-starter-web的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
新建一個GreetingController.java文件:
package com.example.demo; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class GreetingController { @GetMapping("/hello") public String sayHello() { return "?? Hello, World!"; } @GetMapping("/greeting/{name}") public String greetUser(@PathVariable String name) { return "?? Hello, " + name + "! Welcome to Spring Boot!"; } }
用瀏覽器訪問:127.0.0.1:8080/api/greeting/jack
就能看到:?? Hello, jack! Welcome to Spring Boot!
訪問:127.0.0.1:8080/api/hello
就能看到:?? Hello, World!
記得直接訪問,前面不要加https。

浙公網安備 33010602011771號