diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9fbb8c6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "type": "java", + "name": "Spring Boot-ConsumingWebServiceApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.example.consumingwebservice.ConsumingWebServiceApplication", + "projectName": "consuming-web-service-initial", + "args": "", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e012065 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "java.compile.nullAnalysis.mode": "automatic", + "java.configuration.updateBuildConfiguration": "interactive" +} \ No newline at end of file diff --git a/complete/pom.xml b/complete/pom.xml index 3874e53..5caf373 100644 --- a/complete/pom.xml +++ b/complete/pom.xml @@ -52,6 +52,7 @@ 3.0.0 + wsimport diff --git a/initial/pom.xml b/initial/pom.xml index 2c9c5f3..93cb620 100644 --- a/initial/pom.xml +++ b/initial/pom.xml @@ -9,18 +9,34 @@ com.example - consuming-web-service-initial + consuming-web-service-complete 0.0.1-SNAPSHOT - consuming-web-service-initial + consuming-web-service-complete Demo project for Spring Boot + 17 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot spring-boot-starter-web-services + + + org.springframework.boot + spring-boot-starter-tomcat + + + org.springframework.boot @@ -35,7 +51,31 @@ org.springframework.boot spring-boot-maven-plugin + + + com.sun.xml.ws + jaxws-maven-plugin + 3.0.0 + + + + + wsimport + + + + + com.example.consumingwebservice.wsdl + + http://localhost:8080/ws/countries.wsdl + + ${sourcesDir} + ${classesDir} + true + + + - + \ No newline at end of file diff --git a/initial/src/main/java/com/example/consumingwebservice/ConsumingWebServiceApplication.java b/initial/src/main/java/com/example/consumingwebservice/ConsumingWebServiceApplication.java index 0afc9ba..03f312d 100644 --- a/initial/src/main/java/com/example/consumingwebservice/ConsumingWebServiceApplication.java +++ b/initial/src/main/java/com/example/consumingwebservice/ConsumingWebServiceApplication.java @@ -1,13 +1,30 @@ package com.example.consumingwebservice; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import com.example.consumingwebservice.wsdl.GetCountryResponse; @SpringBootApplication public class ConsumingWebServiceApplication { - public static void main(String[] args) { - SpringApplication.run(ConsumingWebServiceApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ConsumingWebServiceApplication.class, args); + } + + @Bean + CommandLineRunner lookup(CountryClient countryClient) { + return args -> { + String country = "Spain"; + + if (args.length > 0) { + country = args[0]; + } + GetCountryResponse response = countryClient.getCountry(country); + System.err.println(response.getCountry().getCurrency()); + }; + } -} +} \ No newline at end of file diff --git a/initial/src/main/java/com/example/consumingwebservice/CountryClient.java b/initial/src/main/java/com/example/consumingwebservice/CountryClient.java new file mode 100644 index 0000000..edba042 --- /dev/null +++ b/initial/src/main/java/com/example/consumingwebservice/CountryClient.java @@ -0,0 +1,31 @@ +package com.example.consumingwebservice; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.ws.client.core.support.WebServiceGatewaySupport; +import org.springframework.ws.soap.client.core.SoapActionCallback; + +import com.example.consumingwebservice.wsdl.GetCountryRequest; +import com.example.consumingwebservice.wsdl.GetCountryResponse; + +public class CountryClient extends WebServiceGatewaySupport { + + private static final Logger log = LoggerFactory.getLogger(CountryClient.class); + + public GetCountryResponse getCountry(String country) { + + GetCountryRequest request = new GetCountryRequest(); + request.setName(country); + + log.info("Requesting location for " + country); + + GetCountryResponse response = (GetCountryResponse) getWebServiceTemplate() + .marshalSendAndReceive("http://localhost:8080/ws/countries", request, + new SoapActionCallback( + "http://spring.io/guides/gs-producing-web-service/GetCountryRequest")); + + return response; + } + +} \ No newline at end of file diff --git a/initial/src/main/java/com/example/consumingwebservice/CountryConfiguration.java b/initial/src/main/java/com/example/consumingwebservice/CountryConfiguration.java new file mode 100644 index 0000000..f436953 --- /dev/null +++ b/initial/src/main/java/com/example/consumingwebservice/CountryConfiguration.java @@ -0,0 +1,28 @@ +package com.example.consumingwebservice; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.oxm.jaxb.Jaxb2Marshaller; + +@Configuration +public class CountryConfiguration { + + @Bean + public Jaxb2Marshaller marshaller() { + Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); + // this package must match the package in the specified in + // pom.xml + marshaller.setContextPath("com.example.consumingwebservice.wsdl"); + return marshaller; + } + + @Bean + public CountryClient countryClient(Jaxb2Marshaller marshaller) { + CountryClient client = new CountryClient(); + client.setDefaultUri("http://localhost:8080/ws"); + client.setMarshaller(marshaller); + client.setUnmarshaller(marshaller); + return client; + } + +} \ No newline at end of file