在这段代码中我可以使用什么来代替try-catch异常处理

4smxwvx5  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(485)

关闭。这个问题需要详细或明确。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,添加细节并澄清问题。

三天前关门。
改进这个问题
我可以在这段代码中使用什么来代替try-catch异常处理?我尝试使用JavaThrows关键字来声明异常,而不是try-catch异常处理,但它没有在测试用例中传递。这里还有其他处理异常的方法吗?我试图删除一些重复的代码,我用***标记了重复的代码(我在8个方法中有相同的代码),但它使用了自己的try块中的class对象,如果我创建了一个方法并在那里使用try-catch块,那么我必须传递的参数仍然在try块中,在最后一行我已经注解掉了

AjaxResponseBean moveControl(@PathVariable Integer formId, @RequestBody AjaxRequestBean request) {
        try {
            Form form = formService.getFormWithDraft(formId);
            FormDraftData draft = formService.getFormWithDraft(formId).getDraft();

            String formXml = draft.getData();
            String id = request.getData().getControls().get(0).getId();
            String beforeId = request.getData().getControls().get(0).getBeforeId();

            JAXBContext jaxbControlContext = JAXBContext.newInstance(XmlFormBean.class);
            Unmarshaller jaxbControlUnmarshaller = jaxbControlContext.createUnmarshaller();
            XmlFormBean xmlFormBean = (XmlFormBean) jaxbControlUnmarshaller.unmarshal(new StringReader(formXml));

            XmlFormBean.Control xmlControl = new XmlFormBean.Control(id);
            for (XmlFormBean.Control c : xmlFormBean.getControls()) {
                if (c.getId().equalsIgnoreCase(id)) {
                    xmlControl = c;
                    break;
                }
            }

            xmlFormBean.getControls().remove(xmlControl);
            int index = -1;
            if (StringUtils.isNotBlank(beforeId)) {
                index = xmlFormBean.getControls().indexOf(new XmlFormBean.Control(beforeId));
            }
            if (index == -1) {
                xmlFormBean.getControls().add(xmlControl);
            } else {
                xmlFormBean.getControls().add(index, xmlControl);
            }
//******************************
       Marshaller jaxbControlMarshaller = jaxbControlContext.createMarshaller();
            StringWriter writer = new StringWriter();
            jaxbControlMarshaller.marshal(xmlFormBean, writer);
            form.getDraft().setData(writer.toString());
            formService.addForm(form);

            // AjaxResponseBean
            AjaxResponseBean response = new AjaxResponseBean();
            response.setResult("success");
            response.setData(request.getData());
            return response;

        } catch (JAXBException e) {
            e.printStackTrace();
        }

        AjaxResponseBean response = new AjaxResponseBean();
        AjaxDataBean data = new AjaxDataBean();
        data.setMessage("The client request failed.");
        response.setResult("failure");
        response.setData(data);
        //return MarshallerSomething(form, jaxbControlContext, xmlFormBean, request);
        return response;
//******************************
    }
}
83qze16e

83qze16e1#

您可以在params中提到jaxbcontext的超类,并根据方法要求传递子类,这样即使需要不同的实现,也可以将公共代码取出并移动到不同的方法。
假设上下文是jaxbcontext的超类,jcontext是它的子类。

public void testMethod(Context con, String str){
//your logic here
}

Context c1 = new JAXBContext()
Context c2 = new JContext() 
testMethod(c1, "test1")
testMethod(c2, "test2")

您可以尝试以下方法,但不会出现编译时异常

AjaxResponseBean moveControl(@PathVariable Integer formId, @RequestBody AjaxRequestBean request) {
        Form form = null;
        JAXBContext jaxbControlContext= null;
        XmlFormBean xmlFormBean = null;
            try {
                form = formService.getFormWithDraft(formId);
                FormDraftData draft = formService.getFormWithDraft(formId).getDraft();

                String formXml = draft.getData();
                String id = request.getData().getControls().get(0).getId();
                String beforeId = request.getData().getControls().get(0).getBeforeId();

                jaxbControlContext = JAXBContext.newInstance(XmlFormBean.class);
                Unmarshaller jaxbControlUnmarshaller = jaxbControlContext.createUnmarshaller();
                xmlFormBean = (XmlFormBean) jaxbControlUnmarshaller.unmarshal(new StringReader(formXml));

                XmlFormBean.Control xmlControl = new XmlFormBean.Control(id);
                for (XmlFormBean.Control c : xmlFormBean.getControls()) {
                    if (c.getId().equalsIgnoreCase(id)) {
                        xmlControl = c;
                        break;
                    }
                }

                xmlFormBean.getControls().remove(xmlControl);
                int index = -1;
                if (StringUtils.isNotBlank(beforeId)) {
                    index = xmlFormBean.getControls().indexOf(new XmlFormBean.Control(beforeId));
                }
                if (index == -1) {
                    xmlFormBean.getControls().add(xmlControl);
                } else {
                    xmlFormBean.getControls().add(index, xmlControl);
                }
//******************************
                Marshaller jaxbControlMarshaller = jaxbControlContext.createMarshaller();
                StringWriter writer = new StringWriter();
                jaxbControlMarshaller.marshal(xmlFormBean, writer);
                form.getDraft().setData(writer.toString());
                formService.addForm(form);

                // AjaxResponseBean
                AjaxResponseBean response = new AjaxResponseBean();
                response.setResult("success");
                response.setData(request.getData());
                return response;

            } catch (JAXBException e) {
                e.printStackTrace();
            }

            AjaxResponseBean response = new AjaxResponseBean();
            AjaxDataBean data = new AjaxDataBean();
            data.setMessage("The client request failed.");
            response.setResult("failure");
            response.setData(data);
            return MarshallerSomething(form, jaxbControlContext, xmlFormBean, request);
            //return response;
//******************************
        }

相关问题