The solution is to create a Factory Class responsible for instanciate the client.
Other common requirement is to read the service endpoing from a config file, so it could vary from enviroment.
Factory code:
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import es.sisifo.test.ws.webservices.UsuarioWebService;
import es.sisifo.test.ws.webservicesimpl.UsuarioService;
public class UsuariosWebServiceFactoryBean implements FactoryBean<UsuarioWebService> {
private final String namespaceUri;
private final String serviceName;
private final String wsdlLocation;
private final String serviceEndPoint;
public UsuariosWebServiceFactoryBean(final String namespaceUri, final String serviceName, final String wsdlLocation, final String serviceEndPoint) {
this.namespaceUri = namespaceUri;
this.serviceName = serviceName;
this.wsdlLocation = wsdlLocation;
this.serviceEndPoint = serviceEndPoint;
}
@Override
public UsuarioWebService getObject() throws Exception {
final QName serviceQName = new QName(namespaceUri, serviceName);
final Resource serviceWSDLResource = new ClassPathResource(wsdlLocation);
final UsuarioService service = new UsuarioService(serviceWSDLResource.getURL(), serviceQName);
final UsuarioWebService port = service.getUsuarioWebServiceImplPort();
final BindingProvider bindingProvider = (BindingProvider)port;
bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, serviceEndPoint);
return port;
}
@Override
public Class<?> getObjectType() {
return UsuarioWebService.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
To use this class, you only need to declare this factory as a normal bean, for example, in the application-context.xml:
<bean id="usuarioService" class="es.sisifo.test.usuarios.webservice.UsuariosWebServiceFactoryBean">
<constructor-arg name="namespaceUri" value="${webService.usuarios.namespaceUri}" />
<constructor-arg name="serviceName" value="${webService.usuarios.serviceName}" />
<constructor-arg name="wsdlLocation" value="${webService.usuarios.wsdlDocumentLocation}" />
<constructor-arg name="serviceEndPoint" value="${webService.usuario.serviceEndPoint}" />
</bean>
And this would be the properties file:
webService.usuarios.namespaceUri = http://webservicesimpl.ws.testc.es/ webService.usuarios.serviceName = usuarioService webService.usuarios.wsdlLocation = /wsdl/UsuarioService.wsdl webService.usuarios.serviceEndPoint = http://localhost:8080/test-ws/servizos/UsuarioService
Note that the WSDL file is in the application classpath, i.e, in main/resources folder, inside wsdl directory.
You can check how to create all necesary classes for the client in this post: http://trabajosdesisifo.blogspot.com.es/2015/04/eclipse-maven-configure-wsdl2java.html
No hay comentarios:
Publicar un comentario