求个java实现的webservice返回xml的例子代码 .......VC程序员,无任何web开发经验,赶鸭子上架只能学了目前只想实现一个webservice,读取数据库然后将检索结果构造成xml形式return,客户端自己再解析出内容,很简单的入门级别类,JDBC操作无问题,希望有例子的朋友指点下代码,谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果不涉及到SOAP,用Servlet/JSP直接就可以做了。 import javax.jws.WebMethod;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.xml.ws.Endpoint;@WebService(targetNamespace = "http://jdk.study.hermit.org/client")@SOAPBinding(style = SOAPBinding.Style.RPC)public class Hello{ @WebMethod public String sayHello(String name) { return "hello:" + name; } public static void main(String[] args) { Endpoint.publish("http://localhost:80/hello", new Hello()); }}运行这个程序,然后访问你本机:http://localhost:80/hello?wsdl,可以看到如下页面<?xml version="1.0" encoding="UTF-8"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://jdk.study.hermit.org/client" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://jdk.study.hermit.org/client" name="HelloService"> <types></types> <message name="sayHello"> <part name="arg0" type="xsd:string"></part> </message> <message name="sayHelloResponse"> <part name="return" type="xsd:string"></part> </message> <portType name="Hello"> <operation name="sayHello" parameterOrder="arg0"> <input message="tns:sayHello"></input> <output message="tns:sayHelloResponse"></output> </operation> </portType> <binding name="HelloPortBinding" type="tns:Hello"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding> <operation name="sayHello"> <soap:operation soapAction=""></soap:operation> <input> <soap:body use="literal" namespace="http://jdk.study.hermit.org/client"></soap:body> </input> <output> <soap:body use="literal" namespace="http://jdk.study.hermit.org/client"></soap:body> </output> </operation> </binding> <service name="HelloService"> <port name="HelloPort" binding="tns:HelloPortBinding"> <soap:address location="http://localhost/hello"></soap:address> </port> </service></definitions> 刚才只是如何使用WS,关于XML解析,自己去查DOM4J文档或去网上搜.大把大把的. import javax.jws.WebMethod;import javax.jws.WebService;import javax.jws.soap.SOAPBinding;import javax.xml.ws.Endpoint;@WebService(targetNamespace = "http://jdk.study.hermit.org/client")@SOAPBinding(style = SOAPBinding.Style.RPC)public class Hello{ @WebMethod public String sayHello(String name) { return "hello:" + name; } public static void main(String[] args) { Endpoint.publish("http://localhost:80/hello", new Hello()); }}同意3 楼 大写们来看哈吧!!servlet跳转路径问题!!! 使用复合主键,还是增加一个唯一标识的单列主键? xsd如何绑定java ext3 的treegrid如何点击按钮让所有节点展开 介绍一本关于soap比较好的书 数据统计组件 IIS怎么整合tomcat啊??? 新手关于Struts和JBOSS的问题!!! J2ee的高手们帮忙看一下,如何设定Jboss的默认网站的根页面? WS用的Jetty 各位大牛 JS写的那个dom和xml的dom啥区别啊 求救 下面代码哪里错了 做Struts_Hibernate时遇到的问题,求高人指点
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;@WebService(targetNamespace = "http://jdk.study.hermit.org/client")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Hello
{
@WebMethod
public String sayHello(String name)
{
return "hello:" + name;
}
public static void main(String[] args)
{
Endpoint.publish("http://localhost:80/hello", new Hello());
}
}
运行这个程序,然后访问你本机:http://localhost:80/hello?wsdl,可以看到如下页面<?xml version="1.0" encoding="UTF-8"?><definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://jdk.study.hermit.org/client" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://jdk.study.hermit.org/client" name="HelloService">
<types></types>
<message name="sayHello">
<part name="arg0" type="xsd:string"></part>
</message>
<message name="sayHelloResponse">
<part name="return" type="xsd:string"></part>
</message>
<portType name="Hello">
<operation name="sayHello" parameterOrder="arg0">
<input message="tns:sayHello"></input>
<output message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloPortBinding" type="tns:Hello">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://jdk.study.hermit.org/client"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://jdk.study.hermit.org/client"></soap:body>
</output>
</operation>
</binding>
<service name="HelloService">
<port name="HelloPort" binding="tns:HelloPortBinding">
<soap:address location="http://localhost/hello"></soap:address>
</port>
</service>
</definitions>
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Endpoint;@WebService(targetNamespace = "http://jdk.study.hermit.org/client")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Hello
{
@WebMethod
public String sayHello(String name)
{
return "hello:" + name;
}
public static void main(String[] args)
{
Endpoint.publish("http://localhost:80/hello", new Hello());
}
}同意3 楼