架构元素引用未定义的类型。创建 SOAP 客户端服务。如何考虑生成的 类 的绑定定制?

Schema element references undefined type. Create SOAP client service. How to take into account binding customization of the generated classes?

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

ERROR 6792 --- [nio-5500-exec-1] o.a.c.w.s.f.ReflectionServiceFactoryBean : Schema element {http://tempuri.org/}SearchMagistratesCourtRequest references undefined type SearchMagistratesCourtRequest for service {http://tempuri.org/}WebServiceService.

这是正确的,因为我生成的 class 的名称为 "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 classes 的绑定定制?或者什么可能是解决方案?

此问题已通过服务的另一次初始化得到解决。绑定工作正常。 因此,在我的案例中,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;
        //          }
        //      }

    }
}