How to change webservice url endpoint?
Asked Answered
C

4

115

I generated a web-service client using JBoss utils (JAX-WS compatible) using Eclipse 'web service client from a wsdl'.

So, the only thing I provided was a url to a web-service WSDL.

Now, the web service provider tells me to change the "url of client endpoint application access" of the web-service.

What is it and how to change it?

Carcajou answered 22/3, 2010 at 8:15 Comment(6)
Can you just recreate the thing using the same Eclipse wizard with the new URL?Beauty
Tell web service provider you need the new url to wsdl, then use it with Eclipse wizard to regenerate the client.Overstride
@Beauty @systemputoout GUYS, the problem is that they have THE SAME WSDL URL !! I'm not sure, but it seems to me that in Axis you can provide a URL when invoking the web service. In JAX-WS you cannot change the "client endpoint during runtime". Any ideas, guys?Carcajou
I must be missing something then and will remove my answer (I don't understand what the "client endpoint" is, it doesn't make any sense to me).Galbreath
@ Pascal Thivent, @Overstride Cite: "URL or endpoint for client application access"Carcajou
Well, my understanding of this sentence is "clients access a service endpoint; the endpoint location has changed". And this makes sense.Galbreath
G
195

IMO, the provider is telling you to change the service endpoint (i.e. where to reach the web service), not the client endpoint (I don't understand what this could be). To change the service endpoint, you basically have two options.

Use the Binding Provider to set the endpoint URL

The first option is to change the BindingProvider.ENDPOINT_ADDRESS_PROPERTY property value of the BindingProvider (every proxy implements javax.xml.ws.BindingProvider interface):

...
EchoService service = new EchoService();
Echo port = service.getEchoPort();

/* Set NEW Endpoint Location */
String endpointURL = "http://NEW_ENDPOINT_URL";
BindingProvider bp = (BindingProvider)port;
bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpointURL);

System.out.println("Server said: " + echo.echo(args[0]));
...

The drawback is that this only works when the original WSDL is still accessible. Not recommended.

Use the WSDL to get the endpoint URL

The second option is to get the endpoint URL from the WSDL.

...
URL newEndpoint = new URL("NEW_ENDPOINT_URL");
QName qname = new QName("http://ws.mycompany.tld","EchoService"); 

EchoService service = new EchoService(newEndpoint, qname);
Echo port = service.getEchoPort();

System.out.println("Server said: " + echo.echo(args[0]));
...
Galbreath answered 22/3, 2010 at 8:59 Comment(9)
I think that there is an error in the second code block, shouldn't it be URL newEndpoint = new URL("WSDL_URL"); in the first line ??Countermark
here is a link to a tutorial tugdualgrall.blogspot.com/2009/02/…Sternick
It's worth pointing-out that modern wsimport tools do not generate code with a get[Service]Port method any more. Instead, call get[Service] and cast the resulting object to a BindingProvider to set these kinds of properties.Isobath
Thanks @ChristopherSchultz on the wsimport tip! That def. worked for usBarter
The first method is interesting! I'm using Service service = Service.create(soapUrl, qName) to create a web service and then I can call getPort(..) on it. However, when I use the default constructor of the web service there is no method like getPort(..) which I could use! How do you do this? How does your EchoService look like?Carpenter
As far as I understand from cxf generated stub code, the second option above changes wsdl url, not the service url. Am i missing something?Helsell
Thanks @ChristopherSchultz for the clarification. In my impl I couldn't figure out what get[Service]Port() referred to.Fistulous
The workaround for the first method to work all the time is to set location property of the address tag in the wsdl to localhost - it won't throw any errors. It is still useful in a few cases; ie, i need to access the wsdl from project resources, because of 'requirements' and still need to customize the address because there are multiple environments (test, prod, etc)Urochrome
my client is deployed from other server, that's the razon to try set endpoint in other domain or dynacmically, but dont work only take the port but but not the domainGeneralist
P
24

To add some clarification here, when you create your service, the service class uses the default 'wsdlLocation', which was inserted into it when the class was built from the wsdl. So if you have a service class called SomeService, and you create an instance like this:

SomeService someService = new SomeService();

If you look inside SomeService, you will see that the constructor looks like this:

public SomeService() {
        super(__getWsdlLocation(), SOMESERVICE_QNAME);
}

So if you want it to point to another URL, you just use the constructor that takes a URL argument (there are 6 constructors for setting qname and features as well). For example, if you have set up a local TCP/IP monitor that is listening on port 9999, and you want to redirect to that URL:

URL newWsdlLocation = new URL("http://theServerName:9999/somePath");
SomeService someService = new SomeService(newWsdlLocation);

and that will call this constructor inside the service:

public SomeService(URL wsdlLocation) {
    super(wsdlLocation, SOMESERVICE_QNAME);
}
Power answered 19/3, 2015 at 23:31 Comment(3)
Not necessarily. I have services generated with Apache CXF's wsdl2java, and even when we pass the new wsdl location to the constructor, its ports still attempt to bind to the location set at compile/generation time (not leaving any choice but to typecast the port to BindingProvider and set the new address in its request context map.)Suspect
@Luis - Hard to know exactly what you are seeing, but if you debug you should see the call into the javax Provider class, and then see it try to create the endpoint with your new wsdl location (assuming you are using JAX-WS 2.0+). Then inside your service, the getPort call should call super.getPort, which uses has your new port set in a serviceDelegate object. That's how it should work with javax.xml.ws.Service in JAX-WS 2.0. I'd put a breakpoint on the super call and investigate from there.Power
@Power - Whether the WSDL URL we add to the project always needs to be accessible even if we change the URL dynamically? Could you please share the behavior?Jedthus
H
1

I wouldn't go so far as @Femi to change the existing address property. You can add new services to the definitions section easily.

<wsdl:service name="serviceMethodName_2">
  <wsdl:port binding="tns:serviceMethodNameSoapBinding" name="serviceMethodName">
    <soap:address location="http://new_end_point_adress"/>
  </wsdl:port>
</wsdl:service>

This doesn't require a recompile of the WSDL to Java and making updates isn't any more difficult than if you used the BindingProvider option (which didn't work for me btw).

Hypochondriasis answered 27/12, 2018 at 16:10 Comment(0)
S
-8

To change the end address property edit your wsdl file

<wsdl:definitions.......
  <wsdl:service name="serviceMethodName">
    <wsdl:port binding="tns:serviceMethodNameSoapBinding" name="serviceMethodName">
      <soap:address location="http://service_end_point_adress"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
Shenitashenk answered 9/11, 2013 at 10:10 Comment(1)
In many cases, the WSDL is imposed to you and you are not supposed to change it. More importantly, from an environment to another (test vs live), the endpoint url is likely to change.. and nobody wants to tweak the wsdl and recompile in this case.Ziegfeld

© 2022 - 2024 — McMap. All rights reserved.