Commit f2553ac4 authored by Administrator's avatar Administrator

Initial commit

parents
/target/
.classpath
.settings
.project
This diff is collapsed.
# Ejemplos básicos de Servicios Web en Java
Ejemplos de Servicios Web en Java incluidos en el Tema 6.2 de la asignatura DAI.
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>es.uvigo.esei.dai.ws</groupId>
<artifactId>calculator_service</artifactId>
<version>1.0.0</version>
<name>Ejemplos de DAI - Servicios Web: Calculator Service</name>
<inceptionYear>2014</inceptionYear>
<url>https://sing-group.org/dt/gitlab/dai-2324/ws-calculator-service</url>
<developers>
<developer>
<name>Miguel Reboiro-Jato</name>
<organization>Escola Superior de Enxeñaría Informática -
Universidade de Vigo</organization>
<organizationUrl>https://esei.uvigo.es/</organizationUrl>
<email>mrjato@uvigo.gal</email>
</developer>
</developers>
<licenses>
<license>
<name>GNU GENERAL PUBLIC LICENSE, Version 3</name>
<url>http://www.gnu.org/licenses/gpl.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Dependencies versions -->
<jax-ws.version>4.0.1</jax-ws.version>
<!-- Plugin versions -->
<license-maven-plugin.version>2.2.0</license-maven-plugin.version>
<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<!-- license-maven-plugin configuration -->
<license.licenseName>gpl_v3</license.licenseName>
<license.copyrightOwners>Miguel Reboiro Jato</license.copyrightOwners>
<license.organizationName>Universidade de Vigo</license.organizationName>
<license.addJavaLicenseAfterPackage>false</license.addJavaLicenseAfterPackage>
</properties>
<contributors>
<contributor>
<name>Miguel Reboiro Jato</name>
<email>mrjato@uvigo.gal</email>
<organization>Escola Superior de Enxeñaría Informática -
Universidade de Vigo</organization>
<organizationUrl>https://esei.uvigo.es/</organizationUrl>
<roles>
<role>author</role>
<role>professor</role>
</roles>
</contributor>
</contributors>
<dependencies>
<!-- API -->
<!-- Realmente no hace falta esta dependencia, ya que es transitiva de jaxws-rt,
pero hay entornos (p.ej. Jakarta EE/Java EE) en los que se debe importar esta
dependencia y no jaxws-rt, pues esta última será proporcionada por el contenedor.
-->
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>${jax-ws.version}</version>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jax-ws.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>${license-maven-plugin.version}</version>
<executions>
<execution>
<id>first</id>
<goals>
<goal>update-file-header</goal>
</goals>
<phase>process-sources</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package es.uvigo.esei.dai.ws.calculator;
public class CalculatorException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final String faultInfo;
public CalculatorException(String message) {
this(message, message);
}
public CalculatorException(String message, String faultInfo) {
super(message);
this.faultInfo = faultInfo;
}
public String getFaultInfo() {
return faultInfo;
}
}
package es.uvigo.esei.dai.ws.calculator;
import java.util.concurrent.Executors;
import jakarta.xml.ws.Endpoint;
public class CalculatorServer {
/**
* @param args
*/
public static void main(String[] args) {
final Endpoint endpoint = Endpoint.publish(
"http://localhost:9876/calculator",
new CalculatorServiceImpl()
);
endpoint.setExecutor(
Executors.newFixedThreadPool(20)
);
}
}
package es.uvigo.esei.dai.ws.calculator;
import jakarta.jws.WebMethod;
import jakarta.jws.WebService;
@WebService
// @SOAPBinding(style = Style.RPC)
public interface CalculatorService {
@WebMethod
public double add(double op1, double op2);
@WebMethod
public double subtract(double op1, double op2);
@WebMethod
public double multiply(double op1, double op2);
@WebMethod
public double divide(double op1, double op2) throws CalculatorException;
}
package es.uvigo.esei.dai.ws.calculator;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import jakarta.xml.ws.Service;
public class CalculatorServiceClient {
public static void main(String[] args) throws MalformedURLException {
final URL url = new URL("http://localhost:9876/calculator?wsdl");
final QName name = new QName(
"http://calculator.ws.dai.esei.uvigo.es/",
"CalculatorServiceImplService"
);
final Service service = Service.create(url, name);
final CalculatorService calculator = service.getPort(CalculatorService.class);
System.out.println(calculator.add(20d, 30d));
System.out.println(calculator.subtract(20d, 30d));
System.out.println(calculator.multiply(20d, 0d));
System.out.println(calculator.divide(20d, 30d));
try {
System.out.println(calculator.divide(20d, 0d));
} catch (CalculatorException e) {
e.printStackTrace();
}
}
}
package es.uvigo.esei.dai.ws.calculator;
import jakarta.jws.WebService;
@WebService(endpointInterface = "es.uvigo.esei.dai.ws.calculator.CalculatorService")
public class CalculatorServiceImpl implements CalculatorService {
@Override
public double add(double op1, double op2) {
return op1 + op2;
}
@Override
public double subtract(double op1, double op2) {
return op1 - op2;
}
@Override
public double multiply(double op1, double op2) {
return op1 * op2;
}
@Override
public double divide(double op1, double op2) throws CalculatorException {
if (op2 == 0d)
throw new CalculatorException("op2 can't be zero");
return op1 / op2;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment