SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用

介绍

相信许多熟悉spring的小伙伴都经受过spring配置的摧残,尤其对于初学者来说,简直是一脸懵逼的状态。而spring boot 的出现,对于使用spring的童鞋们无疑是一种福音。下面就让我们一起开始spring boot 的体验之旅吧!


IDEA构建项目

新建Spring Initalizr 项目
SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用_第1张图片
点击next(注意包名小写)
SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用_第2张图片
然后选择web工程
SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用_第3张图片
点击finish,即可完成项目构建
SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用_第4张图片
当所有的依赖包下载完成之后,大概是这个样子
SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用_第5张图片

pom.xml



	4.0.0

	com.example
	demo
	0.0.1-SNAPSHOT
	jar

	demo
	Demo project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.2.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


和spring boot 说句hello world 吧

创建DemoController

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by pengq on 2018/5/27 13:20
 * Description:
 */
@RestController
public class DemoController {
    @RequestMapping(name = "/hello",method = RequestMethod.GET)
    public String test(){
        return "hello world";
    }
}

启动spring boot 的启动类DemoApplication

package com.example.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);
	}
}

观察控制台,会打印出如下log:

Starting DemoApplication on DESKTOP-OLPDM22 with PID 12156 (C:\Users\pengq\Documents\demo\target\classes started by pengq in C:\Users\pengq\Documents\demo)
2018-05-27 13:23:03.289  INFO 12156 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-05-27 13:23:03.425  INFO 12156 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5a955565: startup date [Sun May 27 13:23:03 CST 2018]; root of context hierarchy
2018-05-27 13:23:05.349  INFO 12156 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-05-27 13:23:05.411  INFO 12156 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-05-27 13:23:05.412  INFO 12156 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31

我们发现spring boot 内置的Tomcat 访问端口为8080,启动成功后打开浏览器访问http://localhost:8080/hello,如图:
SpringBoot系列(一)基于Idea 使用spring boot创建简单的web应用_第6张图片

到此,我们的一个简单的web项目已经构建成功,是不是很方便呢?熟悉spring 的小伙伴们可能会有疑问。为什么不是熟悉的@Controller却变成了@RestController?不用着急,我们下节会说到。

spring boot 的特点

  1. 快速创建独立的Spring应用程序。
  2. 嵌入的Tomcat,不需要部署war文件。
  3. 简化了Maven的配置。
  4. 开箱即用,没有代码生成、也不需要XML的配置。
  5. 是微服务的入门级框架,SpringBoot是SpringCloud的基础。

由于本人也是刚刚开始学习,可能会有些地方不够严谨,欢迎大家指出,谢谢。

你可能感兴趣的:(spring,boot)