本视频由 保利威 提供技术支持

2. 对接前台

点击获取同步代码

教师列表接口:

GET http://localhost:8089/teacher

接口响应示例:
状态码:200
响应主体

[
  {
    "id": 1,
    "name": "张三",
    "username": "zhangsan",
    "email": "zhangsan@yunzhiclub.com",
    "sex": true
  },
  {
    "id": 2,
    "name": "李四",
    "username": "lisi",
    "email": "lisi@yunzhiclub.com",
    "sex": false
  }
]

核心代码

package club.yunzhi.springcourse.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 教师
 */
@CrossOrigin(origins = "http://localhost:8080/")
@RestController
public class TeacherController {

  @GetMapping("teacher")
  public String getAll() {
    return "[\n" +
        "  {\n" +
        "    \"id\": 1,\n" +
        "    \"name\": \"张三\",\n" +
        "    \"username\": \"zhangsan\",\n" +
        "    \"email\": \"zhangsan@yunzhiclub.com\",\n" +
        "    \"sex\": true\n" +
        "  },\n" +
        "  {\n" +
        "    \"id\": 2,\n" +
        "    \"name\": \"李四\",\n" +
        "    \"username\": \"lisi\",\n" +
        "    \"email\": \"lisi@yunzhiclub.com\",\n" +
        "    \"sex\": false\n" +
        "  }\n" +
        "]";
  }
}