114

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?

EugeneP
  • 11,783
  • 32
  • 96
  • 142
  • Can you just recreate the thing using the same Eclipse wizard with the new URL? – Thilo Mar 22 '10 at 08:28
  • Tell web service provider you need the new url to wsdl, then use it with Eclipse wizard to regenerate the client. – systempuntoout Mar 22 '10 at 08:36
  • @Thilo @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? – EugeneP Mar 22 '10 at 08:53
  • 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). – Pascal Thivent Mar 22 '10 at 09:03
  • 1
    @ Pascal Thivent, @systempuntoout Cite: "URL or endpoint for client application access" – EugeneP Mar 22 '10 at 09:14
  • 1
    Well, my understanding of this sentence is "clients access a service endpoint; the endpoint location has changed". And this makes sense. – Pascal Thivent Mar 22 '10 at 09:25

4 Answers4

190

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]));
...
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 8
    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 ?? – Jaime Hablutzel Mar 26 '12 at 18:21
  • 4
    here is a link to a tutorial http://tugdualgrall.blogspot.com/2009/02/jax-ws-how-to-configure-service-end_17.html – shareef Feb 29 '16 at 19:09
  • 18
    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. – Christopher Schultz Jun 22 '16 at 19:34
  • Thanks @ChristopherSchultz on the wsimport tip! That def. worked for us – Cuga Aug 04 '16 at 11:52
  • 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? – Markus L Oct 12 '16 at 07:24
  • 2
    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? – cacert Dec 15 '16 at 10:01
  • Thanks @ChristopherSchultz for the clarification. In my impl I couldn't figure out what get[Service]Port() referred to. – havoc1 Aug 06 '18 at 17:45
  • 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) – Kamil Bęben Jun 28 '21 at 10:03
  • 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 domain – erod Jul 31 '21 at 01:47
23

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);
}
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
MattC
  • 5,874
  • 1
  • 47
  • 40
  • 4
    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.) – luis.espinal Sep 02 '16 at 17:56
  • 1
    @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. – MattC Sep 03 '16 at 22:26
  • @MattC - 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? – Venkat Feb 10 '21 at 05:00
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).

TastyWheat
  • 2,127
  • 1
  • 13
  • 12
-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>
MKB
  • 7,587
  • 9
  • 45
  • 71
Femi
  • 15
  • 16
    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. – Myobis Dec 16 '13 at 09:56