MsgService.java
package org.example.msgservice;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;/**
 * This class was generated by the JAX-WS RI. JAX-WS RI 2.1.3-hudson-390-
 * Generated source version: 2.0
 * 
 */
@WebService(name = "MsgService", targetNamespace = "http://www.example.org/MsgService/")
public interface MsgService { /**
 * 
 * @param content
 * @param checkcode
 * @param spid
 * @param date
 * @param mobile
 * @return returns java.lang.String
 */
@WebMethod(action = "http://www.example.org/MsgService/NewOperation")
@WebResult(name = "status", targetNamespace = "")
@RequestWrapper(localName = "sendMsg", targetNamespace = "http://www.example.org/MsgService/", className = "org.example.msgservice.SendMsg")
@ResponseWrapper(localName = "sendMsgResponse", targetNamespace = "http://www.example.org/MsgService/", className = "org.example.msgservice.SendMsgResponse")
public String sendMsg(
@WebParam(name = "spid", targetNamespace = "") String spid,
@WebParam(name = "date", targetNamespace = "") String date,
@WebParam(name = "checkcode", targetNamespace = "") String checkcode,
@WebParam(name = "mobile", targetNamespace = "") String mobile,
@WebParam(name = "content", targetNamespace = "") String content);}MsgServiceSOAPImpl.java
package org.example.msgservice;import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;import sun.misc.BASE64Encoder;import com.test.Demo;@javax.jws.WebService(endpointInterface = "org.example.msgservice.MsgService", targetNamespace = "http://www.example.org/MsgService/", serviceName = "MsgService", portName = "MsgServiceSOAP")
public class MsgServiceSOAPImpl { public String sendMsg(String spid, String date, String checkcode,
String mobile, String content) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Not implemented yet.");
}
}
ObjectFactory.java
package org.example.msgservice;import javax.xml.bind.annotation.XmlRegistry;/**
 * This object contains factory methods for each Java content interface and Java
 * element interface generated in the org.example.msgservice package.
 * <p>
 * An ObjectFactory allows you to programatically construct new instances of the
 * Java representation for XML content. The Java representation of XML content
 * can consist of schema derived interfaces and classes representing the binding
 * of schema type definitions, element declarations and model groups. Factory
 * methods for each of these are provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory { /**
 * Create a new ObjectFactory that can be used to create new instances of
 * schema derived classes for package: org.example.msgservice
 * 
 */
public ObjectFactory() {
} /**
 * Create an instance of {@link SendMsgResponse }
 * 
 */
public SendMsgResponse createSendMsgResponse() {
return new SendMsgResponse();
} /**
 * Create an instance of {@link SendMsg }
 * 
 */
public SendMsg createSendMsg() {
return new SendMsg();
}}[email protected](namespace = "http://www.example.org/MsgService/")
package org.example.msgservice;
SendMsg.javapackage org.example.msgservice;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;/**
 * <p>
 * Java class for anonymous complex type.
 * 
 * <p>
 * The following schema fragment specifies the expected content contained within
 * this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="spid" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="date" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="checkcode" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="mobile" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *         &lt;element name="content" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "spid", "date", "checkcode", "mobile",
"content" })
@XmlRootElement(name = "sendMsg")
public class SendMsg { @XmlElement(required = true)
protected String spid;
@XmlElement(required = true)
protected String date;
@XmlElement(required = true)
protected String checkcode;
@XmlElement(required = true)
protected String mobile;
@XmlElement(required = true)
protected String content; /**
 * Gets the value of the spid property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getSpid() {
return spid;
} /**
 * Sets the value of the spid property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setSpid(String value) {
this.spid = value;
} /**
 * Gets the value of the date property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getDate() {
return date;
} /**
 * Sets the value of the date property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setDate(String value) {
this.date = value;
} /**
 * Gets the value of the checkcode property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getCheckcode() {
return checkcode;
} /**
 * Sets the value of the checkcode property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setCheckcode(String value) {
this.checkcode = value;
} /**
 * Gets the value of the mobile property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getMobile() {
return mobile;
} /**
 * Sets the value of the mobile property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setMobile(String value) {
this.mobile = value;
} /**
 * Gets the value of the content property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getContent() {
return content;
} /**
 * Sets the value of the content property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setContent(String value) {
this.content = value;
}}SendMsgResponse.java
package org.example.msgservice;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;/**
 * <p>
 * Java class for anonymous complex type.
 * 
 * <p>
 * The following schema fragment specifies the expected content contained within
 * this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="status" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "status" })
@XmlRootElement(name = "sendMsgResponse")
public class SendMsgResponse { @XmlElement(required = true)
protected String status; /**
 * Gets the value of the status property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getStatus() {
return status;
} /**
 * Sets the value of the status property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setStatus(String value) {
this.status = value;
}}转成6个java文件,我应该调用哪个类的方法来发短信

解决方案 »

  1.   

    http://blog.sina.com.cn/s/blog_5183d2c80100kbvz.html
    你上面是工具生成的代码??
    还是参考上面的写吧。
      

  2.   

    上面的代码是根据wsdl生成的??在开发发工具中
      

  3.   

    上面的代码是根据wsdl生成的??在开发发工具中可以根据wsdl直接生成客户端的
      

  4.   

    楼主用wsdl转的6个java是用来发布webservice服务的,也就说是用来提供给别人调用的,刚好搞反了可以用下面代码测试、参考下
    public class SAAJClientTest {
        public static final String NAMESPACE_URI = "http://www.example.org/MsgService/";   
      
        public static final String PREFIX = "tns";   
      
        private SOAPConnectionFactory connectionFactory;
      
        private MessageFactory messageFactory;   
      
        private URL url;   
      
        public SAAJClientTest(String url) throws SOAPException, MalformedURLException {   
            connectionFactory = SOAPConnectionFactory.newInstance();
            messageFactory = MessageFactory.newInstance();   
            this.url = new URL(url);   
        }   
      
        private SOAPMessage createHelloRequest() throws SOAPException {   
            SOAPMessage message = messageFactory.createMessage();   
            SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();   
            Name helloRequestName = envelope.createName("sendMsg", PREFIX, NAMESPACE_URI);
            SOAPBodyElement helloRequestElement = message.getSOAPBody().addBodyElement(helloRequestName);        helloRequestElement.addChildElement("spid").setTextContent("xxx");
            helloRequestElement.addChildElement("date").setTextContent("2012-08-23");
            helloRequestElement.addChildElement("checkcode").setTextContent("xxx");
            helloRequestElement.addChildElement("mobile").setTextContent("136xxxxxxxx");
            helloRequestElement.addChildElement("content").setTextContent("i like 2 say!");
            return message;
        }   
      
        public void callWebService() throws SOAPException, IOException {   
            SOAPMessage request = createHelloRequest();
            SOAPConnection connection = connectionFactory.createConnection();   
            SOAPMessage response = connection.call(request, url);   
            if (response.getSOAPBody().hasFault()) {   
                SOAPFault fault = response.getSOAPBody().getFault();   
                System.err.println("Received SOAP Fault");   
                System.err.println("SOAP Fault Code :" + fault.getFaultCode());   
                System.err.println("SOAP Fault String :" + fault.getFaultString());   
            }else{
             System.out.println("发送成功");
            }
        }   
      
        public static void main(String[] args) throws Exception {
            String url = "http://www.example.org/MsgService/";
         SAAJClientTest helloClient = new SAAJClientTest(url);   
            helloClient.callWebService();
        }  
    }你也可以使用spring的WebServiceGatewaySupport来调用目标webservice,google一下
      

  5.   

    还有个更简单的方法,楼主可以构建Http报文直接Post以下内容到目标webservice
    也起到调用webservice的效果,注意
    请求头的Content-Type 为 Content-Type: text/xml; charset=utf-8
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><execQuery xmlns="http://ws.myfun7.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <spid xmlns="">xxx</spid>
    <date xmlns="">xxx</date>
    <checkcode xmlns="">xxx</checkcode>
    <mobile xmlns="">xxx</mobile>
    <content xmlns="">xxx</content>
    </execQuery></SOAP-ENV:Body></SOAP-ENV:Envelope>
    常用的URLConnection或 httpclient 都可以实现
      

  6.   

    都给了WSDL了,自然是用工具生成客户端代理,然后直接用了~干嘛还要发数据报
    WS明显的就是屏蔽通讯细节的,使得在分布式环境下,各系统之间功能的调用就跟在本地一样
    你在搞个HTTP什么的,WebService还有什么意义
      

  7.   


    显然我在4楼已经告知楼主编写client程序来调用WS,考虑到楼主通过WSDL工具生成的是WS发布程序还是客户端调用程序都没搞清楚才告知使用http来实现,注:本人稍微有点洁癖,不喜欢工具生成一大堆代码,个人比较喜欢模拟HTTP,轻巧,只是个人爱好,欢迎拍砖
      

  8.   

    我 是电信给我的一个wsdl,我项目中用到,但是我不会,求指导,一般的时候,都是有一个客户端的,但是我生成的java类中没有这个类,我该怎么做呢?
      

  9.   

    我 是电信给我的一个wsdl,我项目中用到,但是我不会,求指导,一般的时候,都是有一个客户端的,但是我生成的java类中没有这个类,我该怎么做呢?
      

  10.   

    我 是电信给我的一个wsdl,我项目中用到,但是我不会,求指导,一般的时候,都是有一个客户端的,但是我生成的java类中没有这个类,我该怎么做呢?
      

  11.   


    你生成的代码是发布webservice,而不是客户端我机器的myeclipse 是可以直接生成客户端代码的,不过我以前装过WS插件,不知道你有没有也有其他转换工具参考:http://download.csdn.net/detail/hhl_program/3974527 
      

  12.   

    那个我可以用WSDL2java直接生成6个类,但是不会使用,而电信给我的事例中有MsgServiceClient.java的类,而我只有一个接口,说明确一点,可能是初始化问题,如果您有时间的话,可以加我QQ510880278帮下我,我给你发下那个文档
      

  13.   

    电信给出的事例代码
    package com.yaoda.bull.ws;import java.net.MalformedURLException;
    import java.util.Collection;
    import java.util.HashMap;import javax.xml.namespace.QName;import org.codehaus.xfire.XFireRuntimeException;
    import org.codehaus.xfire.aegis.AegisBindingProvider;
    import org.codehaus.xfire.annotations.AnnotationServiceFactory;
    import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
    import org.codehaus.xfire.client.XFireProxyFactory;
    import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
    import org.codehaus.xfire.service.Endpoint;
    import org.codehaus.xfire.service.Service;
    import org.codehaus.xfire.soap.AbstractSoapBinding;
    import org.codehaus.xfire.transport.TransportManager;import com.yaoda.bull.util.SysUtils;public class MsgServiceClient { private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
    private HashMap endpoints = new HashMap();
    private Service service0; public MsgServiceClient() {
    create0();
    Endpoint MsgServiceLocalEndpointEP = service0.addEndpoint(
    new QName("http://www.example.org/MsgService/",
    "MsgServiceLocalEndpoint"), new QName(
    "http://www.example.org/MsgService/",
    "MsgServiceLocalBinding"), "xfire.local://MsgService");
    endpoints.put(new QName("http://www.example.org/MsgService/",
    "MsgServiceLocalEndpoint"), MsgServiceLocalEndpointEP);
    Endpoint MsgServiceSOAPEP = service0.addEndpoint(new QName(
    "http://www.example.org/MsgService/", "MsgServiceSOAP"),
    new QName("http://www.example.org/MsgService/",
    "MsgServiceSOAP"), SysUtils.getSmsProperty("msg_url"));
    endpoints.put(new QName("http://www.example.org/MsgService/",
    "MsgServiceSOAP"), MsgServiceSOAPEP);
    } public Object getEndpoint(Endpoint endpoint) {
    try {
    return proxyFactory.create((endpoint).getBinding(), (endpoint)
    .getUrl());
    } catch (MalformedURLException e) {
    throw new XFireRuntimeException("Invalid URL", e);
    }
    } public Object getEndpoint(QName name) {
    Endpoint endpoint = ((Endpoint) endpoints.get((name)));
    if ((endpoint) == null) {
    throw new IllegalStateException("No such endpoint!");
    }
    return getEndpoint((endpoint));
    } public Collection getEndpoints() {
    return endpoints.values();
    } private void create0() {
    TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance()
    .getXFire().getTransportManager());
    HashMap props = new HashMap();
    props.put("annotations.allow.interface", true);
    AnnotationServiceFactory asf = new AnnotationServiceFactory(
    new Jsr181WebAnnotations(), tm, new AegisBindingProvider(
    new JaxbTypeRegistry()));
    asf.setBindingCreationEnabled(false);
    service0 = asf.create((com.yaoda.bull.ws.MsgService.class), props);
    {
    AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,
    new QName("http://www.example.org/MsgService/",
    "MsgServiceLocalBinding"),
    "urn:xfire:transport:local");
    }
    {
    AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,
    new QName("http://www.example.org/MsgService/",
    "MsgServiceSOAP"),
    "http://schemas.xmlsoap.org/soap/http");
    }
    } public MsgService getMsgServiceLocalEndpoint() {
    return ((MsgService) (this)
    .getEndpoint(new QName("http://www.example.org/MsgService/",
    "MsgServiceLocalEndpoint")));
    } public MsgService getMsgServiceLocalEndpoint(String url) {
    MsgService var = getMsgServiceLocalEndpoint();
    org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
    return var;
    } public MsgService getMsgServiceSOAP() {
    return ((MsgService) (this).getEndpoint(new QName(
    "http://www.example.org/MsgService/", "MsgServiceSOAP")));
    } public MsgService getMsgServiceSOAP(String url) {
    MsgService var = getMsgServiceSOAP();
    org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
    return var;
    }}
      

  14.   

    jdk中自带工具wsimport.exe
    直接生成class文件,打个jar包,丢到工程依赖库中,神马代码都看不到的,直接调用就是了
      

  15.   

    wsdl转java客户端代码,可以用axis2。在myeclipse中装axis2插件就可以了,不过光有wsdl貌似还不行,因为有些数据类型是定义在xsd里的,万一wsdl引用的是xsd里的类型,那就找不到啦。。