[Java2023] 请求和响应案例-员工列表

69 阅读1分钟

注意事项:

  • 1.如果导入dom4j显示红色, 在右侧maven刷新
  • 2.课件文件在黑马程序员公众号的资料中,转百度云下载
  • 3.主要就是导入文件复杂一点

image.png

  • 完成上面文件导入后: 1.创建EmpController
package com.itheima.controller;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.utils.XmlParserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class EmpController {
    @RequestMapping("listEmp")
    public Result list()
    {
        // 1.加载并解析xml
        String file = this.getClass().getClassLoader().getResource("emp.xml").getFile();
        System.out.println(file);
        List<Emp> empList = XmlParserUtils.parse(file, Emp.class);
        // 2.数据处理
        empList.stream().forEach(emp -> {
            String gender = emp.getGender();
            if ("1".equals(gender)) {
                emp.setGender("男");
            } else if ("2".equals(gender)) {
                emp.setGender("女");
            }
            String job = emp.getJob();
            if ("1".equals(gender)) {
                emp.setGender("讲师");
            } else if ("2".equals(gender)) {
                emp.setGender("班主任");
            } else if ("3".equals(gender)) {
                emp.setGender("就业指导");
            }
        });
        // 3.响应数据
        return Result.success(empList);
    }
}
  • 2.postman中测试: localhost:8080/listEmp

    结果: 返回了Json

  • 3.浏览器中访问

image.png