加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

jaxws

发布时间:2020-12-16 23:34:53 所属栏目:安全 来源:网络整理
导读: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
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;
	}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读