Simple maven setup to create JAX-WS web service

I would like to create a Maven project that will build my web service in a war

file, ready to be deployed to GlassFish
. Sounds like a piece of cake, however I can't find a tutorial that explains how to do this. Most of them are outdated (circa 2008).

I am using latest Java (1.8), Glassfish (4.1) and Maven (3.3.3).

The goal is to run the "hello world" web service on Glassfish.

code:

import javax.jws.WebService;

@WebService
public class Hello {
    public String sayHello(String name) {
        return "Hello " + name + "!";
    }
}

      

Which maven plugin should I use?

+3


source to share


1 answer


I think you need an annotation @WebMethod

. As far as setting up maven goes, here's a minimal project I deployed in 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 glass in 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 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 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>

      

The complete project including folder structure and basic unit test has moved to github .

+5


source







All Articles