I'd like to create Maven project which will build my webservice to war
file, ready to be deployed on GlassFish. Looks like piece of cake, however I can't find any tutorial that would explain how to do that. Most of them are outdated (around 2008).
I'm using latest Java (1.8), GlassFish (4.1) and Maven (3.3.3).
Goal is to get "hello world" web service running on GlassFish .
Code:
import javax.jws.WebService;
@WebService
public class Hello {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}
What Maven plugin should I use?
I think you need @WebMethod
annotation as well. As for the Maven setup, here's a minimal project I got deployed to GlassFish 4.1 (full profile):
pom.xml
:
<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>com.scotth</groupId>
<artifactId>mvnjaxws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mvnjaxws</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.xml.ws</groupId>
<artifactId>jaxws-api</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Service endpoint implementation class (SampleService.java
):
package com.scotth.mvnjaxws;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class SampleService {
@WebMethod
public String sayHello(String name) {
return String.format("Hello, %s", name);
}
}
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
After deploying to local GlassFish to context /mvnjaxws
, WSDL from http://localhost:8080/mvnjaxws/SampleServiceService?wsdl:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Published by JAX-WS RI (http://jax-ws.java.net). RI's version is Metro/2.3.1-b419 (branches/2.3.1.x-7937; 2014-08-04T08:11:03+0000) JAXWS-RI/2.2.10-b140803.1500 JAXWS-API/2.2.11 JAXB-RI/2.2.10-b140802.1033 JAXB-API/2.2.12-b140109.1041 svn-revision#unknown. -->
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is Metro/2.3.1-b419 (branches/2.3.1.x-7937; 2014-08-04T08:11:03+0000) JAXWS-RI/2.2.10-b140803.1500 JAXWS-API/2.2.11 JAXB-RI/2.2.10-b140802.1033 JAXB-API/2.2.12-b140109.1041 svn-revision#unknown. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://mvnjaxws.scotth.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://mvnjaxws.scotth.com/" name="SampleServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://mvnjaxws.scotth.com/" schemaLocation="http://localhost:8181/mvnjaxws/SampleServiceService?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<portType name="SampleService">
<operation name="sayHello">
<input wsam:Action="http://mvnjaxws.scotth.com/SampleService/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://mvnjaxws.scotth.com/SampleService/sayHelloResponse" message="tns:sayHelloResponse"/>
</operation>
</portType>
<binding name="SampleServicePortBinding" type="tns:SampleService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="SampleServiceService">
<port name="SampleServicePort" binding="tns:SampleServicePortBinding">
<soap:address location="http://localhost:8181/mvnjaxws/SampleServiceService"/>
</port>
</service>
</definitions>
Full project including folder structure and basic unit test pushed to GitHub.