gpt4 book ai didi

java - Jax-Ws 解码错误

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

我在使用 Jax-Ws 时遇到了问题,而且在网上搜索了一天之后,我没有找到有效的解决方案。我在本地 jboss eap7 上运行我的 Soap Ws。

我的 wsdl 的相关片段如下所示:

<xs:complexType name="simpleTravelingDay">
<xs:sequence>
<xs:element name="gid" type="xs:string"/>
<xs:element name="dayType" type="xs:long"/>
<xs:element name="date" type="xs:dateTime"/>
<xs:element name="projectId" type="xs:long"/>

我的网络服务看起来像这样:

@WebService(name = "TravelTrackerWS")
public interface TravelTrackerWSLocal {

@WebMethod(operationName = "fillSimpleTravelingDays")
public WsAnswer fillSimpleAndTravelingDays(
@XmlElement(required = true, nillable = false) @WebParam(name = "SimpleAndTravelingDays") List<SimpleTravelingDay> days)
throws InsufficientRightsException;

如果我做这样的请求:

  <soapenv:Header/>
<soapenv:Body>
<ser:fillSimpleTravelingDays>
<!--1 or more repetitions:-->
<SimpleAndTravelingDays>
<gid>Z0030UDK</gid>
<date>2014-10-31</date>
<country>AU</country>
<projectId>a</projectId>
</SimpleAndTravelingDays>
</ser:fillSimpleTravelingDays>

我得到一个解码错误,这是正确的,因为“a”是一个字符串而不是长。

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: For input string: "a"</faultstring>
</soap:Fault>

我现在的问题是。我如何捕获解码错误,以便抛出一般错误消息而不是解码错误。

我希望有人能帮助我

最佳答案

您可以使用 ValidationEventHandler 更改错误消息。

例如:

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.helpers.DefaultValidationEventHandler;

public class MyValidationEventHandler extends DefaultValidationEventHandler {
@Override
public boolean handleEvent(ValidationEvent event) {
if (event.getSeverity() == ValidationEvent.WARNING) {
return super.handleEvent(event);
} else {
throw new RuntimeException("My custom message");
}
}
}

要配置您的端点以使用此处理程序,您可以添加一个将处理程序插入上下文的拦截器。

public class ValidationInterceptor extends AbstractPhaseInterceptor<Message> {


public ValidationInterceptor() {
super(Phase.READ);
}

public void handleMessage(Message message) throws Fault {
message.setContextualProperty("jaxb-validation-event-handler", new MyValidationEventHandler());

}

}

最后你的端点看起来像:

@WebService(endpointInterface="org.jboss.test.schemavalidation.TestWS")
@InInterceptors(classes = {ValidationInterceptor.class})
public class TestWSImpl implements TestWS{

public Integer sum(Integer a, Integer b) {
return a + b;
}

}

在 JBoss 7 中,您必须将依赖项添加到 cxf:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<dependencies>
<module name="org.apache.cxf" services="import" />
</dependencies>
</deployment>
</jboss-deployment-structure>

您可以在以下位置查看完整示例:https://github.com/fedesierr/schemavalidation-ws

关于java - Jax-Ws 解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058636/

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