傻瓜式学JAVA---springboot入门
功能:浏览器发送hello请求,服务器接收并处理,响应hello spring
字符串
一、创建Maven工程
![1cb92e5934dfc935baaea284da307a24.png](https://i-blog.csdnimg.cn/blog_migrate/d162bb920158c598588ca660c49f1e6d.jpeg)
创建项目:springboot_hello
![da90d6b4fc0c925bf03dcb4e099c7c9b.png](https://i-blog.csdnimg.cn/blog_migrate/e1ce35307e983a4b0ecf912882e67631.png)
![3d49fe7a5e0880b5c897118e41b1b658.png](https://i-blog.csdnimg.cn/blog_migrate/cee1dcf7b0765e31fd3aab34f769da4e.png)
选择自佛那个导入jar包
![54ecf08ad752b226d6de50b1ad372ab1.png](https://i-blog.csdnimg.cn/blog_migrate/a93a900345c3b28e1d4ab0a1ea126001.jpeg)
选择设置Maven配置
![d6d741c1eab7b9925b2196f88fd70436.png](https://i-blog.csdnimg.cn/blog_migrate/d4e557f1201ddb1a11a67dfb6e392086.jpeg)
修改Maven配置
![43a106164dbafd1b5e13df9db3e7cd1d.png](https://i-blog.csdnimg.cn/blog_migrate/9273d648061f566d8b2f01ae0d4494ae.jpeg)
二、导入Spring boot相关依赖
使用Maven创建项目,需要导入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependency>
![ab1baa1771612e0706c860e297499439.png](https://i-blog.csdnimg.cn/blog_migrate/6412d53c19e822e362f33a4bdca379b1.jpeg)
三、编写主程序
1.创建主程序
![8d1f2a609c00f7fc4cd3784558fd528e.png](https://i-blog.csdnimg.cn/blog_migrate/7b5bf7620258f41562eacae4eb7c90d2.jpeg)
package com.learn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//SpringBootApplication标注主程序类
@SpringBootApplication
public class HelloSpringMainApplication {
public static void main(String[] args) {
//Spring 应用启动
SpringApplication.run(HelloSpringMainApplication.class,args);
}
}
四、编写相关Controller、Service
package com.learn.com.learn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello springboot";
}
}
五、启动springboot项目
![5c7fa75f317822233f3e6d67d404f4c8.png](https://i-blog.csdnimg.cn/blog_migrate/67d652250ff31ef0c213d86cda4c3c96.jpeg)
项目启动端口为 8080
网页访问路径:http://localhost:8080/hello
![7a5fe03735cf2e11d04cdea36dbf1c17.png](https://i-blog.csdnimg.cn/blog_migrate/7821bdde6baab9f97f8cbbcd769048e1.png)
六、应用打包
加入依赖:将应用打包成一个可执行的jar包
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
![afbc824c9baf9c0a4abecfd35128df62.png](https://i-blog.csdnimg.cn/blog_migrate/1bb4eb04a666f4355eea9868a61ecad9.jpeg)
七、运行程序包
运行JAR
包:
命令:Java -jar springboot_hello-1.0-SNAPSHOT.jar
![e50ddddebb12c6f8fd8aaab1c77eb327.png](https://i-blog.csdnimg.cn/blog_migrate/7788aaebc7bd365dd2d19c80ff14e121.jpeg)
八、使用IDEA自动创建Springboot项目
![d36c6eb9ca760cbf37ae6c20b02fe8ce.png](https://i-blog.csdnimg.cn/blog_migrate/e0cdb17e8d48634fc717053d8a99f702.jpeg)
![4ef1c6b8865c7b28cd767f08c89462f2.png](https://i-blog.csdnimg.cn/blog_migrate/65b0e30488fa96af8d3e965758e5cd9f.jpeg)
![e773acce6ab2b2fa39c4c392ad775ae8.png](https://i-blog.csdnimg.cn/blog_migrate/5273a7d02d75a5dc319e9064a4ab4670.jpeg)
![8a11983b2bbbcb326765a4f5a273fed1.png](https://i-blog.csdnimg.cn/blog_migrate/82d370aebf96913ec9364a6654493e4c.png)
![ee9d9aeb20d890e94e105a1e07bc8297.png](https://i-blog.csdnimg.cn/blog_migrate/5ff3fd8d37d01ea5ff556de35b353c4a.jpeg)
编写Controller
package com.learn.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class hello {
@ResponseBody
@RequestMapping("/hd")
public String Hello(){
return "hello Spriongboot";
}
}
启动运行:
![520e932b63e49bfea14f053be2d2c8cc.png](https://i-blog.csdnimg.cn/blog_migrate/b428b964c3dc26f45a9491728841443d.png)
欢迎关注公众号---后台服务器开发,更多精彩等你来看~