file ->new->web service project
project name随便定义在这里定义为webservice
接下点击next
说明:如果你创建的服务端和客户端在一个project,把第二个http client包也选中,全选也没错的。
点击完成就会看到所创建的工程,在该工程下建立两个包,
ITestService接口定义如下
public interface ITestService {
public String getXmlString(String userName,String passWord,String startTime,String endTime);
}
testService定义如下,主要是从本地读取XML文件内容转换为string格式,以便客户端进行接收
package com.net.service.impl; import java.sql.Timestamp;
import java.util.Calendar;
import java.io.IOException;
import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import com.net.service.ITestService; public class TestService implements ITestService{ public String getXmlString(String userName,String passWord,String startTime,String endTime){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
String xmlString = "";
try{
builder = factory.newDocumentBuilder();
Document document = builder.parse("E:/1.xml");
xmlString = toString(document);
}catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return xmlString;
}
static public String toString(Document document) {
String result = null;
if (document != null) {
StringWriter strWtr = new StringWriter();
StreamResult strResult = new StreamResult(strWtr);
TransformerFactory tfac = TransformerFactory.newInstance();
try {
Transformer t = tfac.newTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "gb2312");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
t.transform(new DOMSource(document.getDocumentElement()),strResult);
} catch (Exception e) {
System.err.println("XML.toString(Document): " + e);
}
result = strResult.getWriter().toString();
} return result;
}
public static void main(String[] args){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
String xmlString = "";
try{
builder = factory.newDocumentBuilder();
Document document = builder.parse("E:/1.xml");
xmlString = toString(document);
}catch (ParserConfigurationException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("xmlString="+xmlString);
}
}
services.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>TestService</name>
<namespace>http://com/net/TestService</namespace>
<serviceClass>com.net.service.ITestService</serviceClass>
<implementationClass>com.net.service.impl.TestService</implementationClass>
</service>
</beans>
然后就是服务发布,楼主用的是JBOSS发布的
然后启动本地服务
打开网页输入http://localhost:8080/webservice/services/TestService?wsdl
如果能打开怎表示服务端成功建立。