springboot 框架 - helloword

Posted 爱穿新衣服的姑凉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot 框架 - helloword相关的知识,希望对你有一定的参考价值。

功能:浏览器发送hello请求,服务器接收请求并处理,返回hello word字符串

一、创建一个maven项目

二、在pom.xml文件中添加依赖导入springboot框架运行需要的依赖

 1     <!-- 继承了一个父项目 -->
 2     <parent>
 3         <groupId>org.springframework.boot</groupId>
 4         <artifactId>spring-boot-starter-parent</artifactId>
 5         <version>2.2.4.RELEASE</version>
 6         <relativePath/> <!-- lookup parent from repository -->
 7     </parent>
 8 
 9     <dependencies>
10         <!-- web模块的依赖 -->
11     <dependency>
12                  <groupId>org.springframework.boot</groupId>
13                  <artifactId>spring-boot-starter-web</artifactId>
14         </dependency>
15     </dependencies>

三、编写一个主程序,自动Springboot

 

 

 

具体启动代码如下:

 1 /**
 2  * @SpringBootApplication 主程序类的标注
 3  */
 4 @SpringBootApplication
 5 public class HelloWordMainApplication {
 6     //main方法快捷键是 psvm
 7     public static void main(String[] args) {
 8         //spring boot 应用启动起来,
 9         SpringApplication.run(HelloWordMainApplication.class,args);
10     }
11 }

四、编写相关的controller,service

  • 新建一个controller类

     

  • 具体代码如下  
    1 @Controller
    2 public class HelloController {
    3 
    4     @ResponseBody  //把接口返回的结果写在浏览器上
    5     @RequestMapping("/hello") //接收来自浏览器的hello请求
    6     public String hello(){
    7         return "Hello Word";
    8     }
    9 }
                                     

五、启动main程序,在浏览器中打开http://localhost:8080/hello 测试结果,结果返回如下

 

以上是关于springboot 框架 - helloword的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot搭建helloword项目(Maven)

springboot2.0 入门程序编写(helloword)

SpringBoot:使用eclipse/idea创建springboot helloword工程

SpringBoot 入门学习(HelloWord)

springboot复习(黑马)(持续更新)

从零开始的Springboot的HelloWord,并实现图片上传功能