[12/20/2019]SpringBoot Demo Hello world 需要用到的包和代码
使用mvn构建项目
需要配置的pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在src/main/resources中配置server运行的端口
application.properties:
server.port = 8084
新建启动器
package:
com.linch.demo:
DemoApplication.java:
package com.linch.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
新建controller
新建package:com.linch.demo.controller
新建class:HelloWorldController.java
package com.linch.demo.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("hello")
public class HelloWorldController {
@RequestMapping("")
public String hello() {
String result = "Hello World";
System.out.println("-------------------");
return result;
}
}
这篇博客介绍了如何使用SpringBoot构建一个简单的Hello World应用。首先,通过配置pom.xml文件来搭建项目,接着在application.properties中设置服务器端口。然后创建启动器DemoApplication,并建立一个新的控制器HelloWorldController。

被折叠的 条评论
为什么被折叠?



