【问题标题】:Simple Maven setup for creating JAX-WS webservice用于创建 JAX-WS Web 服务的简单 Maven 设置
【发布时间】:2015-10-16 15:08:30
【问题描述】:

我想创建 Maven 项目,它将我的 web 服务构建到 war 文件,准备好部署到 GlassFish 上。看起来小菜一碟,但是我找不到任何可以解释如何做到这一点的教程。其中大部分已经过时(2008 年左右)。

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

目标是让“hello world”网络服务在 GlassFish 上运行。

代码:

import javax.jws.WebService;

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

我应该使用什么 Maven 插件?

【问题讨论】:

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


    【解决方案1】:

    我认为您还需要 @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

    【讨论】:

    • 这些依赖够了吗? eclipse 给 webservice 和 webmethod 注释报错
    【解决方案2】:

    我在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多