当前位置: 代码迷 >> Web前端 >> 施用JAX-WS的maven插件发布webservice
  详细解决方案

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

热度:200   发布时间:2013-08-13 16:43:28.0
使用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());
	}
}

?

  相关解决方案