日期:2014-05-17  浏览次数:20914 次

webservice如何发布,怎么得到wsdl?
最近在学webservice ,按照资料自己写了一个demo(java Project)

  贴上代码:
package com.entity;

import java.util.Date;

/**
 * @author jmwu
 *
 */
public class Customer {
private long id;
private String cusName;
private Date cusBirthday;
public long getId() {
return id;
}
/**
* @param id
*/
public void setId(long id) {
this.id = id;
}
public String getCusName() {
return cusName;
}
/**
* @param cusName
*/
public void setCusName(String cusName) {
this.cusName = cusName;
}
public Date getCusBirthday() {
return cusBirthday;
}
/**
* @param cusBirthday
*/
public void setCusBirthday(Date cusBirthday) {
this.cusBirthday = cusBirthday;
}
}

上面这个Customer类是一个方法的返回值类型继续:

服务端点接口:
package com.IService;

import com.entity.Customer;

public interface IHelloService {

Customer selectMaxAgeStudent(Customer c1,Customer c2);
Customer selectMaxLongNameStudent(Customer c1,Customer c2);
}

服务端点接口实现类:
  package com.IService.Impl;

import com.IService.IHelloService;
import com.entity.Customer;

public class IHelloServiceImpl implements IHelloService {

@Override
public Customer selectMaxAgeStudent(Customer c1, Customer c2) {
if (c1.getCusBirthday().getTime()<c2.getCusBirthday().getTime()) 
return c1;
else
return c2;
}

@Override
public Customer selectMaxLongNameStudent(Customer c1, Customer c2) {
if (c1.getCusName().trim().length() >c2.getCusName().trim().length()) 
return c1;
else
return c2;
}

}

web发布:我们发布Web 服务使用的是javax.xml.ws.*包中的EndPoint 的静态方法publish()。

package com.IService.soap;

import javax.xml.ws.Endpoint;

import com.IService.Impl.IHelloServiceImpl;

public class SoapServer {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/helloService",new IHelloServiceImpl());
}
}

资料说我直接运行main方法就可以发布这个web服务,
之后就可以直接访问http://localhost:8080/helloService?wsdl,问题是现在我运行这段程序的时候出现空指针异常,该如何发布这个web服务呢?

异常信息:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.ws.server.EndpointFactory.getWsdlLocation(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.getPrimaryWsdl(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)
at javax.xml.ws.Endpoint.publish(Unknown Source)
at com.IService.soap.SoapServer.main(SoapServer.java:9)



 PS———资料补充:

  Customer类是公开为Web 服务的接口中的参数类型和返回值,因此你需要使用JAXB 注解告诉
CXF 如何在XML和Java Object 之间处理,因为前面说过SOAP 消息格式包装的是一段XML
代码,那么无论是服务器端还是客户端在接收到SOAP 消息时都需要将XML 转化为Java
Object,在发送SOAP 消息时需要将Java Object 转化为XML。

(5.)查看WSDL:
我们访问http://127.0.0.1:8080/helloService?wsdl 地址,您会看到很长的XML 文件(由于浏
览器的问题,如果你看到的是空白页面,请查看源代码),这就是WSDL(WebService Definition
Language),对于你要访问的Web 服务,只要在其地址后加上,就可以在浏览器中查看用于
描述Web 服务的WSDL,这也是一种XML,Web 服务能够被各种编程语言书写的程序访
问就是通过WSDL 这种通用的契约来完成的。
如果你已经看到WSDL,那么表示我们的Web 服务发布成功了。你可能会差异,我们没有
借助Tomcat 这样