一、项目搭建
第一步:点击新建Spring Initializr,然后点击next
第二步:图中已将显示更改的值,然后点击下一步
第三步:我们在这里创建一个web项目,点击web然后点Spring Web然后点击下一步即可。
第四步:如图,直接点击next即可
二、代码展示
第一步:在com.pp下创建一个controller包,然后创建HelloController类
package com.pp.springboothelloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("hello")
public String hello(){
return "hello word";
}
}
使用注解@Controller对类进行注解,说明这是控制层,用 @ResponseBody注解说明将下面注解的方法的以json格式返回到页面,使用@RequestMapping(“hello”)注解意思是当浏览器输入http//127.0
0.1:8080/hello的时候,直接跳转到后台的这里进行处理。方法里面没有进行任何处理之间返回了hello word的字符串内容。
三、运行结果
源码地址:https://gitee.com/yangforever/project-learning/tree/master/demo/SpringBoot/spring-boot-helloworld