首先我们新建一个controller的packages的包,因为SpringBoot的启动类是需要在最高层次的所以我们需要在启动下的新建我们的controller的packages的包这个需要注意
新建之后我们去编写一下controller的代码
package com.msfh.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller//这里我们需要让他知道这是一个控制器
public class FunController {
@RequestMapping("/fun")
@ResponseBody//因为这里我们返回的是字符串
public String fun() {
return "hello SpringBoot";
}
}
之后我们去启动SpringBoot访问一下这个方法
看到了我们返回的字符串了
这样一个基本的Controller就写好了