SpringBoot HelloWorld
2019-03-17 本文已影响0人
MinicupSimon
项目代码:https://github.com/daydreamer1988/springboot-helloworld
方法一:使用Spring Initializer





pom.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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.minicup</groupId>
<artifactId>springboot-helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
// 支持Maven打包
</plugin>
</plugins>
</build>
</project>
Application
@SpringBootApplication
public class SpringbootHelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootHelloworldApplication.class, args);
}
}
以上就是所有的配置,运行main函数

打开浏览器http://localhost:8080

服务是启动了, 但是还没有接口,接下来创建HelloController.java
@RestController
public class HelloController {
@GetMapping("/hello/{name}")
public String hollo(@PathVariable String name){
return "Hello "+ name;
}
}
重新运行

除了运行main函数之外, 还可以通过命令行执行mvn spring-boot:run
来启动

项目打包部署


在命令行中输入java -jar target/springboot-helloworld-0.0.1-SNAPSHOT.jar

注意, pom文件中的依赖没有版本号, 这是因为parent的spring-boot-starter-parent
中已经定义了
方法二:使用普通Maven创建SpringBoot



pom.xml

添加以下依赖(https://spring.io/guides/gs/rest-service/)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<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>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
添加Application类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加TestController类
@RestController
public class TestController {
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "Hello " + name;
}
}
运行main函数,访问8080,完成
方法三:官网下载类似脚手架项目,IDEA直接打开即可
