这里我简单编写一个Hello World的代码
1.设置访问端口
在yml文件中编写端口为8080,我们启动项目是的路径就是localhost:8080
server:
port:8080
2.编写项目代码
在controller包下编写名字democontroller的java文件,代码如下
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class democontroller {
@RequestMapping("/hello")
@ResponseBody
public String hello(){
System.out.println("helloWorld");
return "helloWorld";
}
}
启动类在启动时会做注解扫描(@Controller、@Service、@Repository…),扫描位置为同包或者子包下的注解,所以我们要在启动类同级或同级包下编写代码。
项目启动成功后,访问http://localhost:8888/demo/hello
访问成功后如图所示