You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ## spring-boot-demo-helloworld
  2. > 本 demo 演示如何使用 Spring Boot 写一个hello world
  3. ### 1.开发步骤
  4. #### 1.1.添加依赖
  5. ```xml
  6. <dependencies>
  7. <dependency>
  8. <groupId>com.xkcoding</groupId>
  9. <artifactId>common-tools</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-web</artifactId>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-test</artifactId>
  18. <scope>test</scope>
  19. </dependency>
  20. </dependencies>
  21. ```
  22. #### 1.2.启动类 HelloworldApplication.java
  23. ```java
  24. @SpringBootApplication
  25. @RestController
  26. public class HelloworldApplication {
  27. public static void main(String[] args) {
  28. SpringApplication.run(HelloworldApplication.class, args);
  29. }
  30. /**
  31. * Hello,World
  32. *
  33. * @param who 参数,非必须
  34. * @return Hello, ${who}
  35. */
  36. @GetMapping("/hello")
  37. public String sayHello(@RequestParam(required = false, name = "who") String who) {
  38. if (StrUtil.isBlank(who)) {
  39. who = "World";
  40. }
  41. return StrUtil.format("Hello, {}!", who);
  42. }
  43. }
  44. ```
  45. #### 1.3.配置文件 application.yml
  46. ```yaml
  47. server:
  48. port: 8080
  49. servlet:
  50. context-path: /demo
  51. ```
  52. ### 2.测试
  53. 启动 `HelloworldApplication.java` 打开任意浏览器:
  54. - 输入 `http://localhost:8080/demo/hello` ,输出 `Hello, World!`;
  55. - 输入 `http://localhost:8080/demo/hello?who=xkcoding` ,输出 `Hello, xkcoding!`。