Vállalati Információs Rendszerek

Web szolgáltatás megvalósítások (SOAP és REST)

SOAP

Feladatok:

  • WebServices-JAX-WS mappa tartalmaz egy Java implementációt Hello World jellegű SOAP alapú webszolgáltatáshoz (JAX-WS)
    • Tanulmányozzuk át a kódot
In [11]:
cd WebServices-JAX-WS/src/main/java/vir/soap
vimcat ws/HelloWorld.java
package vir.soap.ws;
 
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
 
//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.DOCUMENT, use=Use.LITERAL)
public interface HelloWorld {
 
        @WebMethod String getHelloWorldAsString(String name);

}
In [12]:
vimcat ws/HelloWorldImpl.java
package vir.soap.ws;
 
import javax.jws.WebService;
 
//Service Implementation
@WebService(endpointInterface = "vir.soap.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
 
        @Override
        public String getHelloWorldAsString(String name) {
                return "Hello World JAX-WS " + name;
        }
 
}
In [13]:
vimcat endpoint/HelloWorldPublisher.java
package vir.soap.endpoint;
 
import javax.xml.ws.Endpoint;

import vir.soap.ws.HelloWorldImpl;
 
//Endpoint publisher
public class HelloWorldPublisher {
 
        public static void main(String[] args) {
           Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
    }
 
}
In [14]:
vimcat client/HelloWorldClient.java
package vir.soap.client;
 
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import vir.soap.ws.HelloWorld;
 
public class HelloWorldClient {
 
        public static void main(String[] args) throws Exception {
 
        URL url = new URL("http://localhost:9999/ws/hello?wsdl");
 
        //1st argument service URI, refer to wsdl document above
        //2nd argument is service name, refer to wsdl document above
        QName qname = new QName("http://ws.soap.vir/", "HelloWorldImplService");
 
        Service service = Service.create(url, qname);
 
        HelloWorld hello = service.getPort(HelloWorld.class);
 
        System.out.println(hello.getHelloWorldAsString("VIR"));
 
    }
 
}
  • Fordítsuk le maven-nel, és Próbáljuk ki 
In [5]:
cd ../../../../..
mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building soap_sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ soap_sample ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ soap_sample ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 4 source files to /home/vir/VIR_notebooks/07/WebServices-JAX-WS/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ soap_sample ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/vir/VIR_notebooks/07/WebServices-JAX-WS/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ soap_sample ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ soap_sample ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ soap_sample ---
[INFO] Building jar: /home/vir/VIR_notebooks/07/WebServices-JAX-WS/target/soap_sample-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.017 s
[INFO] Finished at: 2019-06-19T12:10:35+02:00
[INFO] Final Memory: 16M/196M
[INFO] ------------------------------------------------------------------------

Először futtassuk a web szolgáltatás szerver oldali részét:

In [6]:
java -cp target/soap_sample-1.0-SNAPSHOT.jar vir.soap.endpoint.HelloWorldPublisher

Ha a szerver fut a háttérben, már tudjuk használni a szolgáltatást, meghívhatjuk a klienst az alábbi módon:

In [8]:
java -cp target/soap_sample-1.0-SNAPSHOT.jar vir.soap.client.HelloWorldClient
Hello World JAX-WS VIR
  • Írjunk egy új web szolgáltatást két szám összeadására (getSum(int a, int b)) (5 pont)
    • Egy teljesen új web service interface-t és implementációt hozzunk létre
    • Publikáljunk egy teljesen új endpoint-ot hozzá

REST API

Feladatok:

  • Az alábbi tutorial alapján készítsünk el egy maven alapú Spring Boot alkalmazást, ami egy Hello World szolgáltatást nyújt REST API-n keresztül
  • Az alábbi tutorial alapján készítsünk el egy maven alapú parancssori alkalmazást, ami képes a fenti REST API-t meghívni
  • Írjunk egy új szolgáltatást és használjuk is kliens programból (pl. két szám összeadása)
  • (5+5 pont)