In continuation to previous JAX-WS example where we created GlobalWeatherService web service with one operation "getTemperatureOfCity" and then generated all portal artifacts using wsimport tool.
In this example, we will see how to invoke this web service using web service client.
Create java project in eclipse. Once created, import complete package in.blogspot.ashish4java.globalweatherservice and all classes from that package in this java project.
Create package in.blogspot.ashish4java.globalweatherservice.client
Once done, project structure will be as below,
Create class to invoke web service as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | package in.blogspot.ashish4java.globalweatherservice.client; import java.util.Map; import javax.xml.ws.BindingProvider; import in.blogspot.ashish4java.globalweatherservice.CityNotFoundException_Exception; import in.blogspot.ashish4java.globalweatherservice.CityTemperature; import in.blogspot.ashish4java.globalweatherservice.CityTemperatureImplService; public class MainClient { public static final String city = "Sydney"; public MainClient() { } public static void main(String[] args) { final CityTemperatureImplService cityTempService = new CityTemperatureImplService(); CityTemperature client = cityTempService.getCityTemperatureImplPort(); Map<String, Object> reqContext = ((BindingProvider) client).getRequestContext(); reqContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8080/GlobalWeatherService/CityTemperature?wsdl"); try { String temp = client.getTemperatureOfCity(city); System.out.println("Temperature of " + city + " is - " + temp); } catch (CityNotFoundException_Exception e) { System.out.println("City given is not found -" + e.getMessage()); } } } |
Run this class which gives output successfully -
Temperature of Sydney is -15 Degree Celsius
In next tutorial, we will see how to use handlers to do simple authentication using credentials added to HTTPHeaders.