`
schy_hqh
  • 浏览: 543088 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

使用JAX-WS的maven插件发布webservice

 
阅读更多

1.引入插件过程中,提示miss jar的问题,换中心仓库的地址解决了

settings.xml中将中心仓库的地址改为http://uk.maven.org/maven2

2.基于契约优先的方式,wsdl中的schema如果引用外部的xsd文件,客户端使用wsimport导出会出现问题,提示找不到xsd文件

将xsd中定义的元素拷贝到wsdl中,不要单独定义

 

schema

 

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://ws.student.hqh.com"
	xmlns:tns="http://ws.student.hqh.com" elementFormDefault="qualified">
	
	<xsd:element name="getStudent" type="tns:getStudent" />
	<xsd:element name="getStudentResponse" type="tns:getStudentResponse" />
	<xsd:element name="list" type="tns:list" />
	<xsd:element name="listResponse" type="tns:listResponse" />
	<xsd:element name="listReward" type="tns:listReward" />
	<xsd:element name="listRewardResponse" type="tns:listRewardResponse" />

	<xsd:complexType name="student">
		<xsd:sequence>
			<xsd:element name="id" type="xsd:int" />
			<xsd:element name="name" type="xsd:string" />
			<xsd:element name="sex" type="xsd:string" />
			<xsd:element name="age" type="xsd:int" />
			<xsd:element name="number" type="xsd:string" />
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="reward">
		<xsd:sequence>
			<xsd:element name="id" type="xsd:int" />
			<xsd:element name="year" type="xsd:string" />
			<xsd:element name="rewardInfo" type="xsd:string" />
			<xsd:element name="detail" type="xsd:string" />
			<xsd:element name="student" type="tns:student" />
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="getStudent">
		<xsd:sequence>
			<xsd:element name="number" type="xsd:string" />
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="getStudentResponse">
		<xsd:sequence>
			<xsd:element name="stu" type="tns:student" />
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="list">
		<xsd:sequence />
	</xsd:complexType>

	<xsd:complexType name="listResponse">
		<xsd:sequence minOccurs="1" maxOccurs="unbounded">
			<xsd:element name="students" type="tns:student" />
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="listReward">
		<xsd:sequence>
			<xsd:element name="number" type="xsd:string" />
			<xsd:element name="date" type="xsd:date" />
		</xsd:sequence>
	</xsd:complexType>

	<xsd:complexType name="listRewardResponse">
		<xsd:sequence minOccurs="1" maxOccurs="unbounded">
			<xsd:element name="rewards" type="tns:reward" />
		</xsd:sequence>
	</xsd:complexType>
</xsd:schema>

 

 

wsdl

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://ws.student.hqh.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="StudentWSService"
	targetNamespace="http://ws.student.hqh.com">

	<!-- 指定schema -->
	<wsdl:types>
		<xsd:schema targetNamespace="http://ws.student.hqh.com">
			<!-- <xsd:import schemaLocation="student.xsd" namespace="http://ws.student.hqh.com"/> -->
			<xsd:element name="getStudent" type="tns:getStudent" />
			<xsd:element name="getStudentResponse" type="tns:getStudentResponse" />
			<xsd:element name="list" type="tns:list" />
			<xsd:element name="listResponse" type="tns:listResponse" />
			<xsd:element name="listReward" type="tns:listReward" />
			<xsd:element name="listRewardResponse" type="tns:listRewardResponse" />

			<xsd:complexType name="student">
				<xsd:sequence>
					<xsd:element name="id" type="xsd:int" />
					<xsd:element name="name" type="xsd:string" />
					<xsd:element name="sex" type="xsd:string" />
					<xsd:element name="age" type="xsd:int" />
					<xsd:element name="number" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="reward">
				<xsd:sequence>
					<xsd:element name="id" type="xsd:int" />
					<xsd:element name="year" type="xsd:string" />
					<xsd:element name="rewardInfo" type="xsd:string" />
					<xsd:element name="detail" type="xsd:string" />
					<xsd:element name="student" type="tns:student" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="getStudent">
				<xsd:sequence>
					<xsd:element name="number" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="getStudentResponse">
				<xsd:sequence>
					<xsd:element name="stu" type="tns:student" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="list">
				<xsd:sequence />
			</xsd:complexType>

			<xsd:complexType name="listResponse">
				<xsd:sequence minOccurs="1" maxOccurs="unbounded">
					<xsd:element name="students" type="tns:student" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="listReward">
				<xsd:sequence>
					<xsd:element name="number" type="xsd:string" />
					<xsd:element name="year" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="listRewardResponse">
				<xsd:sequence minOccurs="1" maxOccurs="unbounded">
					<xsd:element name="rewards" type="tns:reward" />
				</xsd:sequence>
			</xsd:complexType>
		</xsd:schema>
	</wsdl:types>

	<!-- 指定方法 -->
	<wsdl:message name="getStudent">
		<wsdl:part element="tns:getStudent" name="parameters" />
	</wsdl:message>
	<wsdl:message name="getStudentResponse">
		<wsdl:part element="tns:getStudentResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="list">
		<wsdl:part element="tns:list" name="parameters" />
	</wsdl:message>
	<wsdl:message name="listResponse">
		<wsdl:part element="tns:listResponse" name="parameters" />
	</wsdl:message>
	<wsdl:message name="listReward">
		<wsdl:part element="tns:listReward" name="parameters" />
	</wsdl:message>
	<wsdl:message name="listRewardResponse">
		<wsdl:part element="tns:listRewardResponse" name="parameters" />
	</wsdl:message>

	<!-- 指定接口 -->
	<wsdl:portType name="IStudentWSService">
		<wsdl:operation name="getStudent">
			<wsdl:input message="tns:getStudent" />
			<wsdl:output message="tns:getStudentResponse" />
		</wsdl:operation>
		<wsdl:operation name="list">
			<wsdl:input message="tns:list" />
			<wsdl:output message="tns:listResponse" />
		</wsdl:operation>
		<wsdl:operation name="listReward">
			<wsdl:input message="tns:listReward" />
			<wsdl:output message="tns:listRewardResponse" />
		</wsdl:operation>
	</wsdl:portType>

	<!-- 指定接口中的方法的参数编码格式 -->
	<wsdl:binding name="studentSOAP" type="tns:IStudentWSService">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="getStudent">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="list">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="listReward">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>

	<!-- 指定服务名称、端口、地址 -->
	<wsdl:service name="StudentWSService">
		<wsdl:port binding="tns:studentSOAP" name="studentServicePort">
			<soap:address location="http://localhost:8080/stu/ws/" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

 安装插件

将插件的执行阶段绑定到了compile期间,只要项目执行编译命令,就会到指定位置寻找wsdl文件并将其转换为java文件。只拷贝接口,然后删除接口中的多余项(方法参数上指定的class)
<!-- jaxws插件 -->
<plugin>
	  <groupId>org.codehaus.mojo</groupId>
	  <artifactId>jaxws-maven-plugin</artifactId>
	  <version>1.9</version>
	  <configuration>
	  	<wsdlDirectory>src/main/resources/META-INF/wsdl</wsdlDirectory>
	  	<wsdlFiles>
	  		<wsdlFile>student.wsdl</wsdlFile>
	  	</wsdlFiles>
	  </configuration>
	  <executions>
	  	<execution>
	  		<phase>compile</phase>
	  		<goals>
	  			<goal>wsimport</goal>
	  		</goals>
	  	</execution>
	  </executions>
</plugin>

 

 

接口

package com.hqh.student.ws;

import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

import com.hqh.student.model.Reward;
import com.hqh.student.model.Student;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.3-b02-
 * Generated source version: 2.1
 * 
 */
@WebService(name = "IStudentWSService", targetNamespace = "http://ws.student.hqh.com")
public interface IStudentWSService {


    /**
     * 
     * @param number
     * @return
     *     returns com.hqh.student.ws.Student
     */
    @WebMethod
    @WebResult(name = "stu", targetNamespace = "")
    @RequestWrapper(localName = "getStudent", targetNamespace = "http://ws.student.hqh.com")
    @ResponseWrapper(localName = "getStudentResponse", targetNamespace = "http://ws.student.hqh.com")
    public Student getStudent(
        @WebParam(name = "number", targetNamespace = "")
        String number);

    /**
     * 
     * @return
     *     returns java.util.List<com.hqh.student.ws.Student>
     */
    @WebMethod
    @WebResult(name = "students", targetNamespace = "")
    @RequestWrapper(localName = "list", targetNamespace = "http://ws.student.hqh.com")
    @ResponseWrapper(localName = "listResponse", targetNamespace = "http://ws.student.hqh.com")
    public List<Student> list();

    /**
     * 
     * @param year
     * @param number
     * @return
     *     returns java.util.List<com.hqh.student.ws.Reward>
     */
    @WebMethod
    @WebResult(name = "rewards", targetNamespace = "")
    @RequestWrapper(localName = "listReward", targetNamespace = "http://ws.student.hqh.com")
    @ResponseWrapper(localName = "listRewardResponse", targetNamespace = "http://ws.student.hqh.com")
    public List<Reward> listReward(
        @WebParam(name = "number", targetNamespace = "")
        String number,
        @WebParam(name = "year", targetNamespace = "")
        String year);

}

 

实现类

 

package com.hqh.student.ws;

import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hqh.student.model.Reward;
import com.hqh.student.model.Student;
import com.hqh.student.service.StudentService;


@WebService(endpointInterface="com.hqh.student.ws.IStudentWSService",
			serviceName="StudentWSService",
			portName="studentServicePort",
			targetNamespace="http://ws.student.hqh.com",
			wsdlLocation="META-INF/wsdl/student.wsdl")
public class StudentWSServiceImpl implements IStudentWSService{
	private static final BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
	public StudentWSServiceImpl() {
		if(studentService==null) {
			studentService = factory.getBean(StudentService.class);
		}
	}
	
	private StudentService studentService;
	
	@Override
	public Student getStudent(String number) {
		return studentService.getStudent(number);
	}

	@Override
	public List<Student> list() {
		return studentService.list();
	}

	@Override
	public List<Reward> listReward(String number, String year) {
		return studentService.listReward(number, year);
	}

}

 

开启服务

 

package com.hqh.student.ws;

import javax.xml.ws.Endpoint;

public class MyLocalServer {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8080/stu/ws", new StudentWSServiceImpl());
	}
}

 

客户端模块

同样引入jaxws插件,绑定到compile期间,根据指定的url地址将wsdl转换为java文件

<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.hqh.student</groupId>
	<artifactId>student-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>student-client</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<!-- jaxws插件 -->
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxws-maven-plugin</artifactId>
				<version>1.9</version>
				<configuration>
					<wsdlUrls>
						<wsdlUrl>http://localhost:8080/stu/ws?wsdl</wsdlUrl>
					</wsdlUrls>
				</configuration>
				<executions>
					<execution>
						<phase>compile</phase>
						<goals>
							<goal>wsimport</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 执行maven:compile命令,将转换后的文件拷贝到src目录下,新建测试类

 

package com.hqh.ws.test;

import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import org.junit.Test;

import com.hqh.student.ws.IStudentWSService;
import com.hqh.student.ws.Reward;
import com.hqh.student.ws.Student;
import com.hqh.student.ws.StudentWSService;

public class TestStudent {
	IStudentWSService iservice = new StudentWSService().getStudentServicePort();
	@Test
	public void testGetStudent() {
		Student stu = iservice.getStudent("20130001");
		System.out.println(stu.getName());
	}
	
	@Test
	public void testList() {
		List<Student> students = iservice.list();
		for(Student stu : students) {
			System.out.println(stu.getName()+","+stu.getSex()+","+stu.getNumber());
		}
	}
	
	@Test
	public void listReward() {
		List<Reward> rewards = iservice.listReward("20130002", "2010");
		for(Reward r : rewards)
			 System.out.println(r.getId()+","+r.getRewardInfo()+","+r.getDetail()+","+r.getStudent().getName());
	}
}

 

分享到:
评论

相关推荐

    JAX-WS发布webservice例子

    JAX-WS发布webservice例子,基于http://blog.csdn.net/yy_love_my/article/details/44938329所说的做的一个简单且能运行的列子,基于maven工程,如果使用servlet发布方式,执行命令:mvn tomcat7:run

    webservice之jax-ws

    NULL 博文链接:https://hgxs-org.iteye.com/blog/1940916

    jax-ws jax-rs 分别创建soap和restful类型的webservice

    使用jax-ws和jax-rs ,maven、java、spring 分别创建soap类型的webservice和restful类型的webservice,文档附有代码

    jaxws-rt 2.1.4 jar java webservice依赖包

    maven 下载jaxws-rt 2.1.4 jar 失败了 其中包含包 com.sun.org.apache.xml.internal resolver 20050927 com.sun.xml.bind jaxb-impl 2.1.7 com.sun.xml.messaging.saaj saaj-impl 1.3.1 ...javax.xml.ws jaxws-api 2.1 ...

    CXF创建webservice服务端.doc

    有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。 4. 支持二进制和遗留协议:CXF 的设计是一种可插...

    CXF的学习笔记

    有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。支持二进制和遗留协议:CXF 的设计是一种可插拨的...

    tutorial-soap-spring-boot-cxf:教程如何使用Spring Boot和Apache CXF创建,测试,部署,监视SOAP-Webservices

    展示如何在构建时使用JAX-WS Commons Maven插件从WSDL生成JAXB类-只需运行 mvn clean generate-sources 首先使用SpringBoot,CXF和JAX-WS运行SOAP-Endpoint。 对于测试,请使用 (在Unittest中测试我们的服务将是...

    simple-webservice

    helloworld-ws:Hello World JAX-WS Web服务作者:亚历山大·克里格(Alexander Krieg) 级别:初学者技术:JAX-WS,Arquillian,Docker,Soapui 摘要:部署捆绑在WAR归档文件中的基本JAX-WS Web服务并运行集成测试...

    soap-to-jpa:生成具有相同结构的 JPA 的 Maven 插件,作为准备保存在数据库中的 SOAP 存根

    Maven 插件soap-to-jpa 生成与 SOAP 存根具有相同结构的 JPA 的 Maven 插件。 它会给你准备好保存在数据库中的实体。 有时您需要请求任何基于 JAX-WS 规范的 API 提供的数据并立即将其放入数据库。 数据用从 WSDL ...

    SoapWS:Spring-WS 与 Spring Boot 一起测试

    JAVA EE 和 JAX-WS 将是一个不错的选择,但我决定坚持使用 Spring WS 和 Spring Boot 作为超级胶水。 作业涵盖了以下主题: 创建两个端点,根据简单过滤返回查询结果列表 使用 WS-Security 进行基本身份验证 按照...

    cxf-spring-boot-starter:由Spring Boot和Apache CXF支持的企业和生产就绪的SOAP Web服务

    使用WSDL / XSD中的JAX-B生成所有必要的Java类(使用补充的Maven插件 使用100%纯Java配置在Spring Context中启动Apache CXF 端点初始化的完全自动化-无需配置Apache CXF端点,所有这些工作都是根据WSDL和生成的...

    BOS技术整理-05

    WebService-WS WS服务独立发布 搭建一个独立的 maven(jar) 服务项目 导入CXF jar 包支持 引入 log4j.properties 文件 @WebService使用类上面,标记类是webservice服务提供对象 @WebMethod使用在方法上面,...

Global site tag (gtag.js) - Google Analytics