Jax-Ws解组错误

Jax-Ws解组错误

问题描述:

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

I'm having an issue with Jax-Ws and also after searching the net for day I found no working solution. I'm running my Soap Ws on a local jboss eap7.

我的wsdl的相关代码段如下所示:

The relevant snippet of my wsdl looks like this:

<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'是一个字符串而不是Long.

I get an Unmarshalling Error, which is correct, because 'a' is a String and not Long.

    <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>

我现在的问题是.如何捕获解组错误",以便引发通用错误消息而不是解组错误".

My Question right now is. How can I catch the Unmarshalling Error, so that I can throw a generic error message instead of the unmarshalling error.

我希望任何人都能帮助我

I hope anyone can help me

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

You can change error message using 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");
        }
    }
}

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

To configure your endpoint to use this handler you can add a interceptor that inserts handler to the context.

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());

    }

}

最后,您的端点如下所示:

Finally your endpoint looks like:

@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:

In JBoss 7 for this works you must add the dependency to 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

You can see a full example in: https://github.com/fedesierr/schemavalidation-ws