傻瓜式学JAVA---springboot入门

367 阅读1分钟

傻瓜式学JAVA---springboot入门

功能:浏览器发送hello请求,服务器接收并处理,响应hello spring字符串

一、创建Maven工程

![](https://imgkr2.cn-bj.ufileos.com/292009b5-e99a-47d9-a442-81cb32c16fc6.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=t03GGYcS2zppD5ubCx0G0JHMKME%253D&Expires=1599959639)
创建项目:springboot_hello
![](https://imgkr2.cn-bj.ufileos.com/a66f7e64-0e04-4feb-9cf9-79ad9bdc367f.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=0cKO7Sl4R3lfjmEK5WXzRdPl4LQ%253D&Expires=1599959645)
![](https://imgkr2.cn-bj.ufileos.com/a9f0c97e-cb2f-4b92-965b-7176df6f0677.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=HRhh9gbTBfrLy07UiOFEKTNRL6s%253D&Expires=1599959651)
选择自佛那个导入jar包
![](https://imgkr2.cn-bj.ufileos.com/394475f3-04d6-48da-b43a-aa8a82f14b95.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=6xjGoB3UwRLqJGAVqe3nbkShpZg%253D&Expires=1599959657)
选择设置Maven配置
![](https://imgkr2.cn-bj.ufileos.com/1bf2f75d-862a-40b9-8914-c4f4b499b06c.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=W%252F9lLRZI5OIBnQBApyGl4uiJm6k%253D&Expires=1599959738)
修改Maven配置
![](https://imgkr2.cn-bj.ufileos.com/1ce4a6cf-22f7-4fe2-a8db-aed5841e7c59.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=ITPNYqHv%252FXHpaDCcicAK8jC1YmY%253D&Expires=1599959754)

二、导入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>
![](https://imgkr2.cn-bj.ufileos.com/5a8f1947-adf5-45cd-83ad-a9c6a248a34c.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=N4D9TmH05RLCOkWY%252BvLT3Kr%252BeAo%253D&Expires=1599959766)

三、编写主程序

1.创建主程序
![](https://imgkr2.cn-bj.ufileos.com/0f3ed066-f839-4347-b9f7-192043f977b6.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=Jn5vSi89vD5MajPaRlX3UkjeU2I%253D&Expires=1599959776)
package com.learn;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;//SpringBootApplication标注主程序类@SpringBootApplicationpublic 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项目

![](https://imgkr2.cn-bj.ufileos.com/4f6b0e78-f66c-4ced-96d8-d49bb4044d6f.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=J8ma1ksUgv1QTNWmQC3Ti1w9kr8%253D&Expires=1599959788)

项目启动端口为 8080

网页访问路径:http://localhost:8080/hello

![](https://imgkr2.cn-bj.ufileos.com/eda61b0f-0026-471b-afe9-3d5b9ed796a0.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=XjRSOrxPunKJxnJo8z5P5OOLhxI%253D&Expires=1599959802)

六、应用打包

加入依赖:将应用打包成一个可执行的jar包

<build>  <plugins>   <plugin>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId>   </plugin>  </plugins> </build>
![](https://imgkr2.cn-bj.ufileos.com/ebedd50d-b1f8-4303-a46b-c031883de665.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=760TC8ZjIF%252BqoAPvTPnI5I66T6w%253D&Expires=1599959838)

七、运行程序包

运行JAR包:

命令:Java -jar springboot_hello-1.0-SNAPSHOT.jar

![](https://imgkr2.cn-bj.ufileos.com/841cb6f9-0740-4e4a-818c-a90e04b5e02a.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=8NvTnjoScNBACXlJqFREl7iKzME%253D&Expires=1599959849)

八、使用IDEA自动创建Springboot项目

![](https://imgkr2.cn-bj.ufileos.com/ec554592-3c88-4eb1-b509-8ea4918979b9.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=48w%252BIqp8BoVvioAa7YM487k1xnQ%253D&Expires=1599959863)
![](https://imgkr2.cn-bj.ufileos.com/2f0682b9-e485-457a-9887-89a8f2fd6ba3.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=f10BgADvaCK4iML%252BbxD%252BObOBY%252FY%253D&Expires=1599959867)
![](https://imgkr2.cn-bj.ufileos.com/fada8ae5-f527-4dd9-87dc-6aaedcd345da.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=wMs%252FU5UdVtmX%252FgZrIX4DSVG50WE%253D&Expires=1599959873)
![](https://imgkr2.cn-bj.ufileos.com/e9bb3ae0-c608-4c40-bf40-d51e42c1ed9d.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=9U66dvAt7skPB7b6pUrSWPugJ28%253D&Expires=1599960011)
![](https://imgkr2.cn-bj.ufileos.com/0dbbb371-76bf-4659-8ff1-0842030c1214.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=fDSCeRTGZYD4WaQfm4QVO1pJv2s%253D&Expires=1599960064)

编写Controller

package com.learn.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class hello {    @ResponseBody    @RequestMapping("/hd")    public String Hello(){        return "hello Spriongboot";    }}

启动运行:

![](https://imgkr2.cn-bj.ufileos.com/8235fd24-4e58-4280-bf72-b306e646e8b4.png?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=bzKVe6CisHfBt2VNtYpR9wi9OBs%253D&Expires=1599960079)

欢迎关注公众号---后台服务器开发,更多精彩等你来看~