I am trying the deploy a web service using an annotated class and Jax-WS. I am deploying it using maven.
The class is just an example program that I have used previously with Java 8. I have recently moved to Java 11 and it does not work any more. I am aware of all the changes with Java 11 and have added a number of different sets of dependencies (I had success previously using com.sun.xml.ws:rt:2.3.1 and javax.xml.ws:jaxws-api:2.3.1 with Java 9).
Can I use Jax-WS in this way with Java 11, or do I need to switch to a top-down approach?
Thanks in advance!
The code:
package quote;
import java.util.Map;
import java.util.TreeMap;
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;
import javax.xml.ws.Endpoint;
@WebService
@SOAPBinding(style = Style.RPC, use= Use.LITERAL)
public class StockPrice {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9000/quote", new StockPrice());
}
private Map<String, Double> prices = new TreeMap<>();
{
prices.put("IBM", 143.79);
prices.put("GOOGL", 1209.70);
prices.put("MSFT", 137.44);
prices.put("FB", 175.25);
prices.put("TWTR", 40.22);
}
@WebMethod public double GetStockQuote(String StockName) {
Double price = prices.get(StockName);
return price == null ? -1:price;
}
}
The pom.xml file:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>examples</groupId>
<artifactId>service</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>9.0.38</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.metro</groupId>
<artifactId>webservices-rt</artifactId>
<version>2.4.4</version>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
<version>2.3.3</version>
</dependency>
<!-- <dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-ri</artifactId>
<version>2.3.1</version>
<type>pom</type>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>quote.StockPrice</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
So, I managed to fix this one myself. It seems that the problem with with the way Web Services are published - the
endpoint.publish(...)
method does not work as previously.To solve the problem, I had to use the following
main()
method:I got this from the "Getting Started with Metro" tutorial: https://javaee.github.io/metro/doc/getting-started/getting-started.html#basma-building
Specifically, I had to download the "From-Java example" code base which included this as a "optional" alternative approach to deploying endpoints.