Java 用于创建 JAX-WS Web 服务的简单 Maven 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31636020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple maven setup for creating JAX-WS webservice
提问by Greg Witczak
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).
我想创建 Maven 项目,它将构建我的 web 服务到war
文件,准备部署在 GlassFish 上。看起来像小菜一碟,但是我找不到任何教程来解释如何做到这一点。它们中的大多数已经过时(大约 2008 年)。
I'm using latest Java (1.8), Glassfish (4.1) and Maven (3.3.3).
我使用的是最新的 Java (1.8)、Glassfish (4.1) 和 Maven (3.3.3)。
Goal is to get "hello world" web service running on Glassfish.
目标是在 Glassfish 上运行“hello world”网络服务。
Code:
代码:
import javax.jws.WebService;
@WebService
public class Hello {
public String sayHello(String name) {
return "Hello " + name + "!";
}
}
What maven plugin should I use?
我应该使用什么 Maven 插件?
采纳答案by Scott Heaberlin
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):
我认为您也需要@WebMethod
注释。至于 maven 设置,这是我部署到 GlassFish 4.1(完整配置文件)的最小项目:
pom.xml:
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):
服务端点实现类(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
网页.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:
部署到本地 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>
Full project including folder structure and basic unit test pushed to github.
完整的项目,包括文件夹结构和基本单元测试推送到 github。
回答by SumiSujith
I deployed this example in wildfly 11
Added jboss-web.xml in WEB-INF.
我在wildfly 11中部署了这个例子
在WEB-INF中添加了jboss-web.xml。
<jboss-web>
<context-root>/mvnjaxws</context-root>
</jboss-web>
After deploying in wildfly to context /mvnjaxws ,WSDL from http://localhost:8080/mvnjaxws/SampleService?wsdl
在wildfly 部署到上下文/mvnjaxws 后,来自http://localhost:8080/mvnjaxws/SampleService?wsdl 的WSDL