微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jaxws

package com.dahuatech.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.soAPBinding;
import javax.jws.soap.soAPBinding.Style;
import javax.jws.soap.soAPBinding.Use;

import com.dahuatech.entity.RestDayMonth;

@WebService(serviceName="HolidayService")
@SOAPBinding(style = Style.RPC,use = Use.LIteraL)
public class HolidayService {

	/**
	 * @return 返回页面节假日的日期,用逗号分隔 如:2014-05-27,2014-05-28,...
	 */
	@WebMethod
	@WebResult(name="result")
	public String getHolidays() {
		List<Date> dates = RestDayMonth.DATES;
		StringBuilder result = new StringBuilder();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		
		int size = dates.size();
		for (int i = 0; i < size; i++) {
			Date d = dates.get(i);
			result.append(dateFormat.format(d));
			
			if (i != size - 1) {
				result.append(",");
			}
		}
		return result.toString();
	}
	
}


web.xml

<servlet>
    	<servlet-name>holidayService</servlet-name>
    	<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
    	<servlet-name>holidayService</servlet-name>
    	<url-pattern>/holidayService</url-pattern>
    </servlet-mapping>


和web.xml同一目录放sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>   
    <endpoint
    name='holidayService'
    implementation='com.dahuatech.service.HolidayService'
    binding="http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/"
    url-pattern='/holidayService'/>
</endpoints>

wsdl:

<deFinitions xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://service.dahuatech.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.dahuatech.com/" name="HolidayService">
<types/>
<message name="getHolidays"/>
<message name="getHolidaysResponse">
<part name="result" type="xsd:string"/>
</message>
<portType name="HolidayService">
<operation name="getHolidays">
<input message="tns:getHolidays"/>
<output message="tns:getHolidaysResponse"/>
</operation>
</portType>
<binding name="HolidayServicePortBinding" type="tns:HolidayService">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="getHolidays">
<soap12:operation soapAction=""/>
<input>
<soap12:body use="literal" namespace="http://service.dahuatech.com/"/>
</input>
<output>
<soap12:body use="literal" namespace="http://service.dahuatech.com/"/>
</output>
</operation>
</binding>
<service name="HolidayService">
<port name="HolidayServicePort" binding="tns:HolidayServicePortBinding">
<soap12:address location="http://plmtest.dahuatech.com:8180/flowQuery/holidayService"/>
</port>
</service>
</deFinitions>


实现:

package com.dahua.handler;

import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.soAPBody;
import javax.xml.soap.soAPConstants;
import javax.xml.soap.soAPEnvelope;
import javax.xml.soap.soAPMessage;
import javax.xml.ws.dispatch;
import javax.xml.ws.Service;

import org.w3c.dom.Document;

import com.dahua.common.LoadConfiguration;

public class HolidayClient {

	
	public List<Date> getHolidays() throws Exception {
		
		URL url = new URL(LoadConfiguration.getHOLIDAYWSDL());
		
		// 创建服务
		String ns = "http://service.dahuatech.com/";
		QName sname = new QName(ns,"HolidayService");
		Service service = Service.create(url,sname);
		
		dispatch<SOAPMessage> dispatch = service.createdispatch(
				new QName(ns,"HolidayServicePort"),SOAPMessage.class,Service.Mode.MESSAGE);
		
		// 创建soap消息
		SOAPMessage msg = MessageFactory.newInstance(SOAPConstants.soAP_1_2_PROTOCOL).createMessage();
        SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
        SOAPBody body = envelope.getBody();
        
        QName ename = new QName(ns,"getHolidays","ser");
        body.addBodyElement(ename);
        
        // 发送请求
        SOAPMessage response = dispatch.invoke(msg);

        // 获取结果
        Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
        String result = doc.getElementsByTagName("result").item(0).getTextContent();

        List<Date> list = new ArrayList<Date>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        
        String[] dates = result.split(",");
        for (String str : dates) {
        	Date date = null;
        	try {
        		date = dateFormat.parse(str);
        	} catch (ParseException e) {
        		e.printstacktrace();
        	}
        	if (date != null) {
        		list.add(date);
        	}
		}
        return list;
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐