SpringBoot创建一个HelloWorld

首先创建一个maven项目,我选择了war,刚创建pom文件头报错,鼠标移到错误提示那块,点击换成低版本的就行.

pom.xml中加入依赖:

<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>
  <groupId>cn.edu.xatu</groupId>
  <artifactId>HelloWorld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
	  <parent>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-parent</artifactId>
	    <version>1.5.6.RELEASE</version>
	  </parent>

	<dependencies>
	    <dependency>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-web</artifactId>
	    </dependency>
	</dependencies>
	
   <build>
  	<plugins>
  		<plugin>
  			<artifactId>maven-war-plugin</artifactId>
  			<configuration>
  				<version>3.0</version>
  			</configuration>
  		</plugin>
  	</plugins>
  </build>
</project>

创建一个Controller类:HelloSpringBoot.java:

package cn.edu.xatu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@SpringBootApplication
@Controller
public class HelloSpringBoot {

	@RequestMapping("hello2")
	@ResponseBody
	public String hello(){
		return "hello world hhhhhhhhh";
	}
	
	public static void main(String[] args) {
		SpringApplication.run(HelloSpringBoot.class, args);
	}
}

直接运行main方法,然后浏览器地址栏输入http://localhost:8080/hello2

这样,一个简单的Hello World已经创建完成了。

现在开始刚才的操作进行分析:

(1)pom文件中继承了spring-boot-starter-parent,引入基本的依赖管理配置。

(2)引入 spring-boot-starter-web,自动引入了SpringWeb相关的包

(3)注解 @SpringBootApplication。这个注解告诉SpringBoot自动的完成相关配置,包括基础类的加载,bean的扫描等等,就是这个标签为我们的应用完成很多基本功能。

(4)SpringApplication.run(HelloSpringBoot.class, args); SpringBoot不再是一个web应用,需要我们自己去打包,部署,启动tomcat , springboot默认把tomcat打包到应用中,我们可以以正常的运行jar的方式来运行springboot应用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章