Java 控制器类中的Spring初始化错误接口

Java 控制器类中的Spring初始化错误接口,java,spring,model-view-controller,Java,Spring,Model View Controller,我在项目中运行的一些验证有问题。我试图验证我在控制器中创建的对象中的一些参数,首先接收一个字符串 @RequestMapping(value = "/publicar", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_XML_VALUE) @ResponseBody public ApiPubPortalPublicarOut publicar(@RequestParam(

我在项目中运行的一些验证有问题。我试图验证我在控制器中创建的对象中的一些参数,首先接收一个字符串

@RequestMapping(value = "/publicar", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ApiPubPortalPublicarOut publicar(@RequestParam(value = PARAM_XML) String xml, Locale locale) {
            //This object is my return, it creates an XML with the validation.
            ApiPubPortalPublicarOut xmlTaxOut = new ApiPubPortalPublicarOut();
            Errors result = null; //Here is the problem

            //validate incoming xml is empty
            if ((xml == null) || (xml.length() == 0)) {
                xmlTaxOut.setDescription("xml is Empty!");
                return xmlTaxOut;
            }else{
                try{
                    //I transform the xml into an object
                    JAXBContext jc = JAXBContext.newInstance(ApiPubPortalPublicarPortal.class);
                    Unmarshaller unmarshaller = jc.createUnmarshaller();
                    StreamSource streamSource = new StreamSource(new StringReader(xmlEntrada));
                    JAXBElement<ApiPubPortalPublicarPortal> je = unmarshaller.unmarshal(streamSource, ApiPubPortalPublicarPortal.class);

                    //Here is the validation method.
                     parsingPublicacion(je.getValue(), result, locale);
                     if(result.hasErrors()){
                        xmlTaxOut.setDescription(result.getAllErrors().toString());
                        return xmlTaxOut;
                     }
                }catch(Exception){
                    xmlTaxOut.setDescription("Error parsing!");
                    return xmlTaxOut;
                }
            }
        }
我无法从该方法调用错误,因为我只允许调用控制器中的特定参数

You can Binding Result & FieldErrors instead of Errors. Please find the below code.

private void parsingPublicacion(ApiPubPortalPublicarPortal portalPublicado,  BindingResult bindingResult, Locale locale) {
    ApiPubPortalPublicarPortal pubPortal = portalPublicado;
    String[] codes = {"errorCode"};
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "name", "name.empty"));
    if (pubPortal.getNombre().length() > 50){
        e.rejectValue("name", "name.oversize");
        bindingResult.addError(new FieldError(Yourclass.getSimpleName(),"Name", pubPortal.getNombre(), false , codes , null , "name.oversize"));
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(e, "idLanguage.empty");
    if ((pubPortal.getIdPortal() == 0)){
        e.rejectValue("idLanguage", "idLanguage.zero"));
        bindingResult.addError(new `enter code here`FieldError(Yourclass.getSimpleName(),"idLanguage", pubPortal.getIdPortal(), false , codes , null , "idLanguage.zero"));
    }

}
编辑2因为您没有模型属性,所以需要使用实现错误的具体类。就是这样

您可以使用BeanPropertyBindingResult如下

@RequestMapping(value = "/publicar", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ApiPubPortalPublicarOut publicar(@RequestParam(value = PARAM_XML) String xml, Locale locale) {
    //This object is my return, it creates an XML with the validation.
    ApiPubPortalPublicarOut xmlTaxOut = new ApiPubPortalPublicarOut();


    //validate incoming xml is empty
    if ((xml == null) || (xml.length() == 0)) {
        xmlTaxOut.setDescription("xml is Empty!");
        return xmlTaxOut;
    }else{
        try{
            //I transform the xml into an object
            JAXBContext jc = JAXBContext.newInstance(ApiPubPortalPublicarPortal.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StreamSource streamSource = new StreamSource(new StringReader(xmlEntrada));
            JAXBElement<ApiPubPortalPublicarPortal> je = unmarshaller.unmarshal(streamSource, ApiPubPortalPublicarPortal.class);

            // Using concrete implementation of error interface
            BeanPropertyBindingResult result = new BeanPropertyBindingResult(je.getValue(), "apiPubPortal");


            //Here is the validation method.
            parsingPublicacion(je.getValue(), result, locale);
            if(result.hasErrors()){
                xmlTaxOut.setDescription(result.getAllErrors().toString());
                return xmlTaxOut;
            }
        }catch(Exception){
            xmlTaxOut.setDescription("Error parsing!");
            return xmlTaxOut;
        }
    }
}
BindingResult扩展了错误,因此您将在其中具有错误功能


我试过了,它抛出:
java.lang.IllegalStateException一个Errors/BindingResult参数应该在模型属性、@RequestBody或@RequestPart参数之后立即声明:public
哦,我明白了!您应该有一个ModelAttribute才能使其工作。如果你没有,你可以使用BeanPropertyBindingResult,它是一个具体的类,你可以直接使用它。。。我需要添加一些参数来初始化它,这让我有点困惑。我需要什么论据?@AngelJaramillo更新2应该可以。让我知道,它有效!非常感谢你!更新2就是答案。主要的问题是如何初始化绑定结果,因为我的控制器不能将其用作方法参数,因为它需要模型属性。
@RequestMapping(value = "/publicar", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ApiPubPortalPublicarOut publicar(@RequestParam(value = PARAM_XML) String xml, Locale locale) {
    //This object is my return, it creates an XML with the validation.
    ApiPubPortalPublicarOut xmlTaxOut = new ApiPubPortalPublicarOut();


    //validate incoming xml is empty
    if ((xml == null) || (xml.length() == 0)) {
        xmlTaxOut.setDescription("xml is Empty!");
        return xmlTaxOut;
    }else{
        try{
            //I transform the xml into an object
            JAXBContext jc = JAXBContext.newInstance(ApiPubPortalPublicarPortal.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            StreamSource streamSource = new StreamSource(new StringReader(xmlEntrada));
            JAXBElement<ApiPubPortalPublicarPortal> je = unmarshaller.unmarshal(streamSource, ApiPubPortalPublicarPortal.class);

            // Using concrete implementation of error interface
            BeanPropertyBindingResult result = new BeanPropertyBindingResult(je.getValue(), "apiPubPortal");


            //Here is the validation method.
            parsingPublicacion(je.getValue(), result, locale);
            if(result.hasErrors()){
                xmlTaxOut.setDescription(result.getAllErrors().toString());
                return xmlTaxOut;
            }
        }catch(Exception){
            xmlTaxOut.setDescription("Error parsing!");
            return xmlTaxOut;
        }
    }
}
@RequestMapping(value = "/publicar", method = { RequestMethod.GET, RequestMethod.POST }, produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public ApiPubPortalPublicarOut publicar(@RequestParam(value = PARAM_XML) String xml,
                                        BindingResult result, Locale locale) {
            //This object is my return, it creates an XML with the validation.
            ApiPubPortalPublicarOut xmlTaxOut = new ApiPubPortalPublicarOut();
            // Use BindingResult in places of erros


        }