gpt4 book ai didi

java - Spring在 Controller 类中初始化Errors接口(interface)

转载 作者:行者123 更新时间:2023-12-02 11:14:50 24 4
gpt4 key购买 nike

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

@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;
}
}
}

这是我的 Controller ,正如您所看到的,我尝试将字符串转换为对象,然后调用方法 parsingPublicacion 来进行验证。

主要问题是我无法初始化我的 Errors 参数,因为它是一个接口(interface),有人知道我如何管理此验证吗?

这是 mi validator 方法。

private void parsingPublicacion(ApiPubPortalPublicarPortal portalPublicado,  Errors e, Locale locale) {
ApiPubPortalPublicarPortal pubPortal = portalPublicado;

ValidationUtils.rejectIfEmptyOrWhitespace(e, "name", "name.empty"));
if (pubPortal.getNombre().length() > 50){
e.rejectValue("name", "name.oversize");
}

ValidationUtils.rejectIfEmptyOrWhitespace(e, "idLanguage.empty");
if ((pubPortal.getIdPortal() == 0)){
e.rejectValue("idLanguage", "idLanguage.zero"));
}

}

我无法从方法中调用错误,因为我只允许调用 Controller 中的特定参数。

最佳答案

Edit 2 Since you don't have a model attribute you need to use a concrete class that implements Errors. BeanPropertyBindingResult is such one.

您可以按如下方式使用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;
}
}
}

Edit 1 You can use Error interface as method parameter too. And Spring will still populate an implementation.

使用BindigResults作为方法参数并在错误的地方使用它。 Spring 将自动为您填充一个实现。

@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


}

BindingResult 扩展了 Errors,因此您将拥有 error 的功能。

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments

关于java - Spring在 Controller 类中初始化Errors接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50367638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com