今天看了一些web service的东西, java实现web service的功能比较简单,一个小例子
MyWebService类
import javax.jws.*;
import javax.jws.soap.*;
@WebService(targetNamespace="http://localhost:7070/mws")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class MyWebService
{
@WebMethod
@WebResult(name="method01Result")
public String method01(@WebParam(name="userName") String userName)
{
System.out.println("You input name is: " + userName);
return userName;
}
@WebMethod
public void method02()
{
System.out.println("method02");
}
public void method03()
{
System.out.println("method03");
}
}
StartService类
import javax.xml.ws.Endpoint;
public class StartService
{
public static void main(String[] args)
{
Endpoint.publish("http://localhost:7070/mws", new MyWebService());
}
}
MyClient类
package localhost._7070.mws;
public class MyClient
{
public static void main(String[] args)
{
MyWebServiceService mwss = new MyWebServiceService();
MyWebService mws = mwss.getMyWebServicePort();
System.out.println(">>>>>>>>>>>>>" + mws.method01("SunHui"));
mws.method02();
mws.method03();
}
}
对类的编译就不说了
生成一个访问服务的客户端:
D:\>wsimport http://localhost:7070/mws?wsdl parsing WSDL... generating code... D:\>
运行StartService:
D:\>java StartService
运行MyClient:
D:\>java localhost._7070.mws.MyClient
服务器端输出:
You input name is: SunHui method02 method03
客户端输出:
>>>>>>>>>>>>>SunHui
在浏览器输入地址http://localhost:7070/mws?wsdl
则得到以下内容:
<?xml version="1.0" encoding="UTF-8" ?> <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:7070/mws" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://localhost:7070/mws" name="MyWebServiceService"> <types /> <message name="method01"> <part name="userName" type="xsd:string" /> </message> <message name="method01Response"> <part name="method01Result" type="xsd:string" /> </message> <message name="method02" /> <message name="method02Response" /> <message name="method03" /> <message name="method03Response" /> <portType name="MyWebService"> <operation name="method01"> <input message="tns:method01" /> <output message="tns:method01Response" /> </operation> <operation name="method02"> <input message="tns:method02" /> <output message="tns:method02Response" /> </operation> <operation name="method03"> <input message="tns:method03" /> <output message="tns:method03Response" /> </operation> </portType> <binding name="MyWebServicePortBinding" type="tns:MyWebService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" /> <operation name="method01"> <soap:operation soapAction="" /> <input> <soap:body use="literal" namespace="http://localhost:7070/mws" /> </input> <output> <soap:body use="literal" namespace="http://localhost:7070/mws" /> </output> </operation> <operation name="method02"> <soap:operation soapAction="" /> <input> <soap:body use="literal" namespace="http://localhost:7070/mws" /> </input> <output> <soap:body use="literal" namespace="http://localhost:7070/mws" /> </output> </operation> <operation name="method03"> <soap:operation soapAction="" /> <input> <soap:body use="literal" namespace="http://localhost:7070/mws" /> </input> <output> <soap:body use="literal" namespace="http://localhost:7070/mws" /> </output> </operation> </binding> <service name="MyWebServiceService"> <port name="MyWebServicePort" binding="tns:MyWebServicePortBinding"> <soap:address location="http://localhost:7070/mws" /> </port> </service> </definitions>
wsimport命令:
D:\>wsimport -help Usage: wsimport [options] <WSDL_URI> where [options] include: -b <path> specify jaxws/jaxb binding files or additional schemas (Each <path> must have its own -b) -B<jaxbOption> Pass this option to JAXB schema compiler -catalog <file> specify catalog file to resolve external entity references supports TR9401, XCatalog, and OASIS XML Catalog format. -d <directory> specify where to place generated output files -extension allow vendor extensions - functionality not specified by the specification. Use of extensions may result in applications that are not portable or may not interoperate with other implementations -help display help -httpproxy:<host>:<port> specify a HTTP proxy server (port defaults to 8080) -keep keep generated files -p <pkg> specifies the target package -quiet suppress wsimport output -s <directory> specify where to place generated source files -target <version> generate code as per the given JAXWS specification version. version 2.0 will generate compliant code for JAXWS 2.0 spec. -verbose output messages about what the compiler is doing -version print version information -wsdllocation <location> @WebServiceClient.wsdlLocation value Examples: wsimport stock.wsdl -b stock.xml -b stock.xjb wsimport -d generated http://example.org/stock?wsdl D:\>
bean信息:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "MmsInfo", propOrder = { "contenteId", "title", "fileList", "auditTime", "createTime" })
public class MmsInfo {
/** ID. */
@XmlElement(name = "contenteId", required = true)
private int contenteId;
/** 标题. */
@XmlElement(name = "title", required = true)
private String title;
/** 资源文件. */
@XmlElement(name = "fileList", required = true)
private MmsResourceFiles[] fileList;
/** 审核通过时间. */
@XmlElement(name = "auditTime", required = true)
private String auditTime;
/** 上传时间. */
@XmlElement(name = "createTime", required = true)
private String createTime;
/**
* 获得ID
*
* @return ID
*/
public int getContenteId() {
return contenteId;
}
/**
* 设置ID
*
* @param contenteId
* ID
*/
public void setContenteId(int contenteId) {
this.contenteId = contenteId;
}
/**
* 获得标题
*
* @return 标题
*/
public String getTitle() {
return title;
}
/**
* 设置标题
*
* @param title
* 标题
*/
public void setTitle(String title) {
this.title = title;
}
/**
* 获得资源文件
*
* @return 资源文件
*/
public MmsResourceFiles[] getFileList() {
return fileList;
}
/**
* 设置资源文件
*
* @param fileList
* 资源文件
*/
public void setFileList(MmsResourceFiles[] fileList) {
this.fileList = fileList;
}
/**
* 获得审核通过时间
*
* @return 审核通过时间
*/
public String getAuditTime() {
return auditTime;
}
/**
* 设置审核通过时间
*
* @param auditTime
* 审核通过时间
*/
public void setAuditTime(String auditTime) {
this.auditTime = auditTime;
}
/**
* 获得上传时间
*
* @return 上传时间
*/
public String getCreateTime() {
return createTime;
}
/**
* 设置上传时间
*
* @param createTime
* 上传时间
*/
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}