本 demo 演示如何使用 Spring Boot 写一个hello world
<dependencies>
<dependency>
<groupId>com.xkcoding</groupId>
<artifactId>common-tools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
@SpringBootApplication
@RestController
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
/**
* Hello,World
*
* @param who 参数,非必须
* @return Hello, ${who}
*/
@GetMapping("/hello")
public String sayHello(@RequestParam(required = false, name = "who") String who) {
if (StrUtil.isBlank(who)) {
who = "World";
}
return StrUtil.format("Hello, {}!", who);
}
}
server:
port: 8080
servlet:
context-path: /demo
启动 HelloworldApplication.java
打开任意浏览器:
http://localhost:8080/demo/hello
,输出 Hello, World!
;http://localhost:8080/demo/hello?who=xkcoding
,输出 Hello, xkcoding!
。