我已将WSDL转换为Java类,但是,我需要使用绑定文件和后缀来解决冲突。我成功地收到了类,但是类型名称略有改变。 当我尝试使用JaxWsProxyFactoryBean创建WebService时,我将原始URL的URL放在具有源名称的URL中,并且它会出现如下错误:
ERROR 6792 --- [nio-5500-exec-1] o.a.c.w.s.f.ReflectionServiceFactoryBean:Schema元素 {http://tempuri.org/} SearchMagistratesCourtRequest引用 未定义类型SearchMagistratesCourtRequest for service {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类的绑定自定义?或者可能是什么解决方案?
答案 0 :(得分: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;
// }
// }
}
}