在实现查找待审批请假单中出现的问题

在实现查找待审批请假单中出现的问题

通过调试,可以在idea中显示出数据库存在的数据:

相关截图:

https://img1.sycdn.imooc.com//climg/61d86f3b0970a8ea13660727.jpg

但是在浏览器中输入http://localhost/api/leave/list?eid=1

却提示500错误。

相关截图:

https://img1.sycdn.imooc.com//climg/61d86f850972c72f13660620.jpg

对于浏览器中提示出现问题的代码:

相关代码:

package com.imooc.oa.controller;

import com.imooc.oa.entity.LeaveForm;
import com.imooc.oa.service.LeaveFormService;
import com.imooc.oa.utils.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/api/leave/*")
public class LeaveFormServlet extends HttpServlet {
    private LeaveFormService leaveFormService = new LeaveFormService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        //http://localhost/api/leave/create
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        if (methodName.equals("create")) {
            this.create(request, response);
        } else if (methodName.equals("list")) {
            this.list(request, response);
        } else if (methodName.equals("audit")) {

        }
    }

    //创建请假单
    private void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strEmployeeId = request.getParameter("eid");
        String formType = request.getParameter("formType");
        //从1970年到现在的毫秒数
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String reason = request.getParameter("reason");
        LeaveForm form = new LeaveForm();
        form.setEmployeeId(Long.parseLong(strEmployeeId));
        form.setStartTime(new Date(Long.parseLong(startTime)));
        form.setEndTime(new Date(Long.parseLong(endTime)));
        form.setFormType(Integer.parseInt(formType));
        form.setReason(reason);
        form.setCreateTime(new Date());
        ResponseUtils resp = null;
        try {
            leaveFormService.createLeaveForm(form);
            resp = new ResponseUtils();
        } catch (Exception e) {
            e.printStackTrace();
            resp = new ResponseUtils(e.getClass().getSimpleName(), e.getMessage());
        }
        response.getWriter().println(resp.toJsonString());
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String employeeId = request.getParameter("eid");
        ResponseUtils resp=null;
        try {
            List<Map> formList = leaveFormService.getLeaveFormList("process", Long.parseLong(employeeId));
            resp=new ResponseUtils().put("list",formList);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resp=new ResponseUtils(e.getClass().getSimpleName(),e.getMessage());
        }
        response.getWriter().println(resp.toJsonString());

    }

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿园 2022-01-08 11:02:32

同学你好,拼接测试同学贴出的代码是可以正常运行的,根据报错提示是在ResponseUtils类的59行出现错误,同学可以检查下该行代码是否正确,另外同学可以检查下pom.xml引入的依赖是否跟课程一致,另外同学检查下导入的包是否正确

祝学习愉快~

  • 提问者 kingzhusr #1

    在ResponseUtils中进行调试出现以下情况

    https://img1.sycdn.imooc.com//climg/61d93195091be62013650728.jpg

    this转换为字符串时报错,第59行提示错误代码是抛出运行时异常:

    throw new RuntimeException(e);

    我将老师文件中的ResponseUtils代码也复制过来运行依然报错。具体ResponseUtils代码如下:

    package com.imooc.oa.utils;
    
    import com.fasterxml.jackson.annotation.JsonInclude;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class ResponseUtils {
        private String code;
        private String message;
        private Map data =new LinkedHashMap<>();
        public ResponseUtils(){
            this.code="0";
            this.message="success";
    
        }
        public ResponseUtils(String code,String message){
            this.setCode(code);
            this.setMessage(message);
        }
        public ResponseUtils put(String key,Object value){
            this.data.put(key,value);
            return this;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    
        public Map getData() {
            return data;
        }
    
        public void setData(Map data) {
            this.data = data;
        }
    
        public String toJsonString(){
            ObjectMapper objectMapper=new ObjectMapper();
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            try {
                String json=objectMapper.writeValueAsString(this);
                return json;
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    }

    对于在pom.xml中的配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.imooc</groupId>
        <artifactId>imooc-oa</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
        </properties>
    
    <!--    引入阿里云路径 开始-->
        <repositories>
            <repository>
                <id>aliyun</id>
                <name>aliyun</name>
                <url>https://maven.aliyun.com/repository/public</url>
            </repository>
        </repositories>
        <pluginRepositories>
            <pluginRepository>
                <id>aliyun</id>
                <name>aliyun</name>
                <url>https://maven.aliyun.com/repository/public</url>
            </pluginRepository>
        </pluginRepositories>
    
        <dependencies>
            <!--引入java servlet依赖-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <!--引入mybatis依赖-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.3</version>
            </dependency>
            <!--引入jdbc驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.26</version>
            </dependency>
            <!--引入junit4单元测试框架-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13.2</version>
                <scope>test</scope>
            </dependency>
            <!--引入logback日志输出组件-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!--Druid数据库连接池-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.2.8</version>
            </dependency>
            <!--Jackson Json处理包-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.12.3</version>
            </dependency>
            <!--Apache 加密解密组件-->
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.15</version>
            </dependency>
        </dependencies>
    </project>


    2022-01-08 15:02:28
  • 好帮手慕阿园 回复 提问者 kingzhusr #2

    同学你好,测试并没有复现同学的问题,同学在pom.xml中添加如下依赖试试

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
        <version>2.6.0</version>
    </dependency>

    如果还不可以,同学可以下载源码,运行源码试试,看看是否会出现该问题

    祝学习愉快~

    2022-01-08 16:10:42
  • 提问者 kingzhusr 回复 好帮手慕阿园 #3

    问题解决了,可是还是不知道问题出在哪里。。。,我先把自己LeaveFormServlet,LeaveForm,ResponseUtils和pom.xml这四个文件先全部注释掉了,然后将老师源代码文件中的这个四个文件的代码复制过来然后运行,显示正常,然后我将自己的先前已经注释掉的代码每恢复一个运行一次,直到最后全部恢复,能够正常运行,期间没有修改任何代码,所以虽然问题解决了,但是不知道原因

    2022-01-08 17:20:02
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师