程序功能要求:浏览器发送hello请求,服务器接受请求并处理,响应hello springboot字符串。
1。第一步创建一个maven工程(jar)
2.导入spring boot依赖的jar包
<?xml version="1.0" encoding="UTF-8"?>
<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>com.zjj</groupId>
<artifactId>springboot01helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<!--spring-boot-starter-parent是一个特殊的starter,提供了一些maven的默认配置,还提供了 dependency——management,(依赖管理),可以是开发者引入其他依赖是不必输入版本号,方便依赖管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<!--要开发web项目需要导入一个web的starter,starter的作用主要是为第三方库提供自动配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--这个插件可以把程序编程一个jar包,无需安装tomcat即可执行-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.编写主程序,启动springboot应用(@SpringBootApplication:来标注一个主程序类,说明这是一个springboot 应用)
package com.zjj;
/**
* Created by 机械师 on 2020/4/5.
*/
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
*@SpringBootApplication:来标注一个主程序类,说明这是一个springboot 应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring 应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4.编写相关的controller,service(知识点: @ResponseBody //想要返回给浏览器用ResponseBody @RequestMapping("/hello") //这个注解时接受浏览器的hello请求)
package com.zjj.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by 机械师 on 2020/4/5.
*/
@Controller
public class Hellocontroller {
@ResponseBody //想要返回给浏览器用ResponseBody
@RequestMapping("/hello")
//这个注解时接受浏览器的hello请求
public String hello(){
return "hello spring boot!";
}
}
5.测试主程序,即运行mian的主程序
6.简化部署,无需tomcat,用spring boot 内置服务器tomcat
部署第一步,导入spring boot的插件在pom中
部署第二步,把本地的springboot项目打成jar包
部署第三步,把本地的jar包放到服务器的桌面上,命令行进入jar包的目录下,java -jar jar包名称 运行,如下图启动29毫秒
7,最后访问浏览器访问hello路径,服务器接受并处理,返回hello springboot字符串