Java Reference
In-Depth Information
Overriding the Endpoint Address in an SEI
Problem
You want to create a JAX-WS client and pass it a different endpoint address at runtime.
Solution
Get the request context from the port and use its map to set the
BindingPro-
vider.ENDPOINT_ADDRESS_PROPERTY
constant as a key with the new endpoint location as its
value.
Discussion
When you generate a service proxy from a WSDL, the endpoint address is hardcoded into
the class. But it is possible to override the endpoint location your client will contact using
constants in the
javax.xml.ws.BindingProvider
interface. This interface defines constants
for endpoint location as well as those for indicating a SOAP action, maintaining session state
across requests, and others.
It's very simple to use, as shown in
Example 6-26
.
Example6-26.Setting the endpoint address property
public class ClientServlet extends HttpServlet {
@WebServiceRef(wsdlLocation=
"http://localhost:8080/CalculatorApp/CalculatorWSService?wsdl")
public CalculatorWSService service;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
org.me.calculator.client.CalculatorWS port =
service.getCalculatorWSPort();
((BindingProvider)port).getRequestContext().put(
BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
"http://localhost:4933/CalculatorApp/CalculatorWSService?wsdl");
int i = Integer.parseInt(request.getParameter("value1"));
int j = Integer.parseInt(request.getParameter("value2"));