Maven创建
首先打开idea,点击“Create New Project”,然后:
接下来再pom.xml中添加如下依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
然后在java目录下创建AppDemo启动类,加上@SpringbootApplication启动注解,该注解包含:@Configuration + @EnableAutoConfiguration + @ComponentScan。代码如下:
@SpringBootApplication public class AppDemo { public static void main(String[] args) { SpringApplication.run(AppDemo.class); } }
接下创建一个接口,代码如下:
@RestController public class HelloDemo { @GetMapping("/hello") public String hello(){ return "hello springboot"; } }
在resources目录上右键点击“new”=>“Resource Bundle”,创建application.properties配置文件,然后可以在里面配置服务器端口,默认的端口是8080。
server.port=8081
最后通过启动类启动项目,访问http:localhost:8081/hello,访问无误的话就会出现下面的页面:
我们的第一个springboot项目就成功了。