Browse Source

hello,world 完成

3.x
Yangkai.Shen 2 years ago
parent
commit
052549905a
5 changed files with 98 additions and 148 deletions
  1. +46
    -90
      demo-helloworld/README.md
  2. +44
    -47
      demo-helloworld/pom.xml
  3. +4
    -4
      demo-helloworld/src/main/java/com/xkcoding/helloworld/Application.java
  4. +3
    -6
      demo-helloworld/src/test/java/com/xkcoding/helloworld/SpringBootDemoHelloworldApplicationTests.java
  5. +1
    -1
      pom.xml

+ 46
- 90
demo-helloworld/README.md View File

@@ -1,106 +1,58 @@
# spring-boot-demo-helloworld
## spring-boot-demo-helloworld


## Runing spring boot demo helloworld

```sh
$ mvn spring-boot:run
```
##
> 本 demo 演示如何使用 Spring Boot 写一个hello world > 本 demo 演示如何使用 Spring Boot 写一个hello world


### pom.xml
```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>

<artifactId>spring-boot-demo-helloworld</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-helloworld</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
### 1.开发步骤


<dependencies>
<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>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>

<build>
<finalName>spring-boot-demo-helloworld</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
#### 1.1.引入关键依赖
```xml
<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>
``` ```


### SpringBootDemoHelloworldApplication.java
#### 1.2.启动类 Application.java


```java ```java
/**
* <p>
* SpringBoot启动类
* </p>
*
* @author yangkai.shen
* @date Created in 2018-09-28 14:49
*/
@SpringBootApplication @SpringBootApplication
@RestController @RestController
public class SpringBootDemoHelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoHelloworldApplication.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);
}
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.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);
}
} }
``` ```


### application.yml
#### 1.3.配置文件 application.yml


```yaml ```yaml
server: server:
@@ -108,4 +60,8 @@ server:
servlet: servlet:
context-path: /demo context-path: /demo
``` ```
### 2.测试


启动 `Application.java` 打开任意浏览器:
- 输入 `http://localhost:8080/demo/hello` ,输出 `Hello, World!`;
- 输入 `http://localhost:8080/demo/hello?who=xkcoding` ,输出 `Hello, xkcoding!`。

+ 44
- 47
demo-helloworld/pom.xml View File

@@ -1,53 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?> <?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" <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"> 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>
<artifactId>demo-helloworld</artifactId>
<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo-helloworld</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<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>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>

<build>
<finalName>demo-helloworld</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>demo-helloworld</artifactId>
<packaging>jar</packaging>

<name>demo-helloworld</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>17</java.version>
</properties>

<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>

<build>
<finalName>demo-helloworld</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project> </project>

demo-helloworld/src/main/java/com/xkcoding/helloworld/SpringBootDemoHelloworldApplication.java → demo-helloworld/src/main/java/com/xkcoding/helloworld/Application.java View File

@@ -9,18 +9,18 @@ import org.springframework.web.bind.annotation.RestController;


/** /**
* <p> * <p>
* SpringBoot启动类
* 启动类
* </p> * </p>
* *
* @author yangkai.shen * @author yangkai.shen
* @date Created in 2018-09-28 14:49
* @date Created in 2022-08-12 20:10
*/ */
@SpringBootApplication @SpringBootApplication
@RestController @RestController
public class SpringBootDemoHelloworldApplication {
public class Application {


public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SpringBootDemoHelloworldApplication.class, args);
SpringApplication.run(Application.class, args);
} }


/** /**

+ 3
- 6
demo-helloworld/src/test/java/com/xkcoding/helloworld/SpringBootDemoHelloworldApplicationTests.java View File

@@ -1,16 +1,13 @@
package com.xkcoding.helloworld; package com.xkcoding.helloworld;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class SpringBootDemoHelloworldApplicationTests {
class ApplicationTests {


@Test @Test
public void contextLoads() {
void contextLoads() {
} }


} }

+ 1
- 1
pom.xml View File

@@ -9,7 +9,7 @@
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
<modules> <modules>
<module>common-tools</module> <module>common-tools</module>
<!-- <module>demo-helloworld</module>-->
<module>demo-helloworld</module>
<!-- <module>demo-properties</module>--> <!-- <module>demo-properties</module>-->
<!-- <module>demo-actuator</module>--> <!-- <module>demo-actuator</module>-->
<!-- <module>demo-admin</module>--> <!-- <module>demo-admin</module>-->


Loading…
Cancel
Save