本文介绍使用springboot快速搭建项目,并返回helloworld串
环境 jdk 1.8 maven:3.3.9 springboot:1.5.9.RELEASE
快速创建maven项目注意是jar类型的项目;
创建完成后引入pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/>
</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
这是springboot搭建web项目基本的配置,
然后创建启动类与controller
其中RootController 是启动类,
@SpringBootApplication
public class RootController {
public static void main(String[] args) {
SpringApplication.run(RootController.class, args);
}
}
这个类一定是在相对跟目录中的,
在看一下HelloController的写法,
@RestController
public class HelloController {
@RequestMapping("test")
public String helloworld() {
return "helloworld";
}
}
直接返回字符串,
springboot是内置tomcat的, 执行启动类的main 方法即可启动项目
之后访问localhost:8080/test 可以看到结果;