用于创建JAX-WS webservice的简单maven设置

时间:2015-07-26 10:42:08

标签: java web-services maven jax-ws maven-archetype

我想创建一个Maven项目,它将我的webservice构建到war文件,准备在GlassFish上部署。看起来像小蛋糕,但我找不到任何可以解释如何做到这一点的教程。他们中的大多数已经过时(大约在2008年)。

我正在使用最新的Java(1.8),Glassfish(4.1)和Maven(3.3.3)。

目标是在Glassfish上运行“hello world”Web服务。

代码:

import javax.jws.WebService;

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

我应该使用什么maven插件?

2 个答案:

答案 0 :(得分:7)

我认为你也需要@WebMethod注释。至于maven设置,这是我部署到GlassFish 4.1(完整档案)的最小项目:

<强>的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>

服务端点实现类(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>

从本地glassfish部署到上下文/mvnjaxws后,来自http://localhost:8080/mvnjaxws/SampleServiceService?wsdl的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>

完整项目,包括文件夹结构和基本单元测试pushed to github

答案 1 :(得分:0)

我在wildfly 11中部署了此示例
在WEB-INF中添加了jboss-web.xml。

 <jboss-web>
  <context-root>/mvnjaxws</context-root>
</jboss-web>

在wildfly中部署到上下文/ mvnjaxws之后,WSDL来自     http://localhost:8080/mvnjaxws/SampleService?wsdl