0

我已经将 WSDL 转换为 Java 类,但是,我需要使用绑定文件和添加的后缀来解决冲突。我成功地收到了课程,但是类型名称略有改变。当我尝试使用 JaxWsProxyFactoryBean 创建 WebService 时,我输入了具有源名称的源 WSDL 的 URL,它给出了如下错误:

错误 6792 --- [nio-5500-exec-1] oacwsfReflectionServiceFactoryBean:架构元素 { http://tempuri.org/ }SearchMagistratesCourtRequest 引用未定义类型 SearchMagistratesCourtRequest 服务 { http://tempuri.org/ }WebServiceService。

这是正确的,因为我生成的类的名称为“SearchMagistratesCourtRequestType” - 最后带有“Type”。

所以我的绑定文件使用了以下自定义:

<jaxb:bindings schemaLocation="../xsd/egrul.xsd">
    <jaxb:schemaBindings>
        <jaxb:package name="ru.spi2.javaee.custom.pravoru.classes.egrul"/>
        <jaxb:nameXmlTransform>
            <jaxb:typeName suffix="Type"/>
            <jaxb:elementName suffix="Element"/>  //this one is not needed actually
        </jaxb:nameXmlTransform>
    </jaxb:schemaBindings>
</jaxb:bindings>

这里使用了后缀。

我像这样创建我的 WebService:

    JaxWsProxyFactoryBean portFactory = new JaxWsProxyFactoryBean();
    portFactory.setAddress(WSDL_URL);
    portFactory.setServiceClass(WebService.class);

    webService = (WebService) portFactory.create();

在这里,我输入了原始 WSDL_URL 并收到了描述的错误。

我如何在此处考虑用于生成 Java 类的绑定定制?或者什么可能是一个解决方案?

4

1 回答 1

0

此问题已通过服务的另一次初始化得到解决。绑定工作正常。因此,在我的情况下,SOAP 服务的正确工作初始化如下:

private void initiateService() throws Exception{

    WebService_Service webService_service = new WebService_Service();

    webService_service.setHandlerResolver(new HandlerResolver() {
        @Override
        public List<Handler> getHandlerChain(PortInfo portInfo) {
            List<Handler> handlerList = new ArrayList<>();
            SOAPLoggingHandler soapLoggingHandler = new SOAPLoggingHandler();
            handlerList.add(soapLoggingHandler);
            return handlerList;
        }
    });

    webServicePort = webService_service.getPort(WebService.class);

    Client client = ClientProxy.getClient(webServicePort);

    Endpoint cxfEndpoint = client.getEndpoint();

    Map<String, Object> props = ((BindingProvider) webServicePort).getRequestContext();
    props.put("ws-security.username", PravoRuConstants.USERNAME);
    props.put("ws-security.password", PravoRuConstants.PASSWORD);

    props.put(WSHandlerConstants.ACTION, WSHandlerConstants.USERNAME_TOKEN);
    props.put(WSHandlerConstants.USER, PravoRuConstants.USERNAME);
    props.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);

    props.put(WSHandlerConstants.PW_CALLBACK_CLASS, PravoRuPasswordHandler.class.getName());

    WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(props);
    cxfEndpoint.getOutInterceptors().add(wssOut);

}

SOAPLoggingHandler:

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import java.io.IOException;
import java.util.Set;

public class SOAPLoggingHandler implements SOAPHandler<SOAPMessageContext> {
    @Override
    public Set<QName> getHeaders() {
        return null;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        SOAPMessage message= context.getMessage();
        boolean isOutboundMessage = (Boolean)context.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if(isOutboundMessage){
            System.out.println("OUTBOUND MESSAGE\n");

        }else{
            System.out.println("INBOUND MESSAGE\n");
        }
        try {
            message.writeTo(System.out);
        } catch (SOAPException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;

    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        SOAPMessage message= context.getMessage();
        try {
            message.writeTo(System.out);
        } catch (SOAPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void close(MessageContext context) { }
}

PravoRuPasswordHandler:

import com.kirillch.constants.PravoRuConstants;
import org.apache.wss4j.common.ext.WSPasswordCallback;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;
import java.io.IOException;

public class PravoRuPasswordHandler implements CallbackHandler {
    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {

        WSPasswordCallback pc = (WSPasswordCallback) callbacks[0];
        pc.setPassword(PravoRuConstants.PASSWORD);


        //      for(int i=0; i<callbacks.length; i++) {
        //          WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
        //          if(pc.getIdentifier().equals(PravoRuConstants.USERNAME)){
        //              pc.setPassword(PravoRuConstants.PASSWORD);
        //              return;
        //          }
        //      }

    }
}
于 2018-02-16T12:09:42.893 回答