我有一个正在运行的 spring-ws 项目,它可以使用 Jax2b 解组请求,但是当整数/布尔值的解组失败时,我会收到一条几乎没有细节的错误消息,而且通常没有无效元素的名称。例如:
org.springframework.oxm.UnmarshallingFailureException: JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.]
这也成为来自我的 Web 服务的 SOAPFault 响应的内容。
我正在尝试更改消息以包含元素名称。我正在使用 ValidationEventHandler 通过从事件处理程序中抛出 RuntimeException 来更改消息,但它仅在某些情况下有效。
验证事件处理程序:
@Component
public class ValidationEventHandlerImpl implements ValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
String message = event.getMessage();
String linkedMessage = "";
if(event.getLinkedException() != null)
linkedMessage = event.getLinkedException().toString();
boolean ignoreValidationEvent = true;
if(message.contains("NumberFormatException") ||
message.contains("is not a valid value") ||
linkedMessage.contains("NumberFormatException") ||
linkedMessage.contains("is not a valid value")){
ignoreValidationEvent = false;
}
if(ignoreValidationEvent){
return true;
}else{
String nodeName = "";
if(event.getLocator() != null && event.getLocator().getNode() != null)
nodeName = event.getLocator().getNode().getNodeName();
//This is the important line
throw new RuntimeException("Error parsing '" + nodeName + "': " + event.getMessage());
}
}
}
它成功地改变了:
JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException: Not a number: 32g321
- with linked exception:
[java.lang.NumberFormatException: Not a number: 32g321]
到: RuntimeException 消息:“ java.lang.RuntimeException:解析“MyNodeName”时出错:不是数字:32g321 ”
(事件严重性:错误)
但是当我想要改变它时它不起作用:
JAXB unmarshalling exception; nested exception is javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; cvc-datatype-valid.1.2.1: '' is not a valid value for 'integer'.]
to: RuntimeException 消息:“ java.lang.RuntimeException:解析 'MyNodeName' 时出错:'' 不是 'integer' 的有效值”。
RuntimeException 被忽略,而 SAXParseException 被抛出并添加到 SOAPFault 响应中。
(事件严重性:FATAL_ERROR)
Jaxb2Marshalling 的 Spring 配置:
<bean id="jaxb2MarshallerContact" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.model.oxm.ContactRequest</value>
<value>com.model.oxm.ContactResponse</value>
</list>
</property>
<property name="marshallerProperties">
<map>
<entry>
<key>
<util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT" />
</key>
<value type="boolean">true</value>
</entry>
<entry>
<key>
<util:constant static-field="javax.xml.bind.Marshaller.JAXB_FRAGMENT" />
</key>
<value type="boolean">true</value>
</entry>
</map>
</property>
<property name="schema" ref="ContactServiceSchema" />
<property name="validationEventHandler" ref="validationEventHandlerImpl" />
</bean>
<bean id="ContactServiceSchema" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="WEB-INF/schemas/ContactService.xsd" />
</bean>
端点:
@Endpoint
public class ContactEndpoint {
private Logger logger = Logger.getLogger(ContactEndpoint.class);
@Autowired
private ContactService contactService;
private static final String NAMESPACE_URI = "http://mydomain/schemas";
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "ContactRequest")
@ResponsePayload
public ContactResponse handleContactRequest(@RequestPayload ContactRequest contactRequest) throws Exception {
...
如何返回自定义消息而不是 SAXParseException 消息?
有没有更好的方法来实现这一点,例如使用 ValidationErrorHandler?
谢谢!