Maven工程在配置文件中添加springboot相关依赖,就能使用注解快速创建springboot工程,生成服务端应用。创建hello world程序查看效果。
目录
程序效果
配置文件修改
注解使用
参考资料
程序效果
截图1.访问URL显示字符
内置tomcat服务器默认使用8080端口,添加程序中配置的“/hello”后缀能访问hello()方法。配置文件修改
pom.xml配置文件中添加org.springframework.boot依赖,会自动下载相关的jar包。
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
dependencies>
注解使用
截图2.程序目录
仅实现2个文件,Application.java文件启动服务,Controller.java文件添加URL请求映射关系。
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
@SpringBootApplication是Sprnig Boot项目的核心注解,目的是开启自动配置。
@RestController
@RequestMapping("/hello")
public class Controller {
@RequestMapping("")
public String hello() {
return "hello world";
}
}
@RestController声明该类是一个控制器,@RequestMapping声明映射关系匹配“/hello”,URL访问“localhost:8080/hello”调用hello()返回字符串“hello world”。
参考资料
1.使用eclipse创建Spring Boot项目 https://www.cnblogs.com/txsblog/p/8566416.html