首先如何生成一个wsdl文档,通过以下的程序暴露一个wsdl文档供大家用
第一步:创建一个接口,该接口有二个函数,一个简单一些,另一个复杂一些。
package yy;
import java.util.List;
import javax.jws.WebService;
import yy.domain.Cat;
import yy.domain.User;
@WebService
public interface HelloWorld {
public String sayHi(String name);
public List
}
第二步:创建一个实现类,去实现该接口:
package yy.impl;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import service.UserService;
import service.impl.UserServiceImpl;
import yy.HelloWorld;
import yy.domain.Cat;
import yy.domain.User;
@WebService(endpointInterface="yy.HelloWorld",serviceName="HelloWorldWS")
public class HelloWorldWS implements HelloWorld{
@Override
public String sayHi(String name) {
return name +" 你好"+"现在的时间是"+new Date();
}
@Override
public List
//zai shijixiangmuzhong ,Web Service 自己并不会去实现业务逻辑,
//而去调用已有的业务逻辑
UserService service=new UserServiceImpl();
return service.getCatsByUser(user);
}
}
package yy.domain;
public class Cat {
private Integer id;
private String name;
private String color;
public Cat() {
}
public Cat(Integer id, String name, String color) {
super();
this.id = id;
this.name = name;
this.color = color;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package yy.domain;
public class User {
private Integer id;
private String name;
private String pass;
private String address;
public User() {
}
public User(Integer id, String name, String pass, String address) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pass == null) {
if (other.pass != null)
return false;
} else if (!pass.equals(other.pass))
return false;
return true;
}
}
package service;
import java.util.List;
import yy.domain.*;
public interface UserService {
public List
}
package service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import service.UserService;
import yy.domain.Cat;
import yy.domain.User;
public class UserServiceImpl implements UserService{
static Map
static{
List
catList1.add(new Cat(1,"garefkds","blcak"));
catList1.add(new Cat(2,"fds","bldascak"));
catDb.put(new User(1,"sun","2233","dad"), catList1);
List
catList2.add(new Cat(3,"garefkds","blcak"));
catList2.add(new Cat(4,"fds","bldascak"));
catDb.put(new User(2,"sun","2233","dad"), catList2);
}
@Override
public List
return catDb.get(user);
}
}
第三部:创建一个发布类
package lee;
import javax.xml.ws.Endpoint;
public class ServerMain {
public static void main(String[] args){
HelloWorld hello=new HelloWorldWS();
//发布WebService
Endpoint.publish("http://IP/webservie" ,hello);
}
}
然后访问http://IP/webservice?wsdl 就可以看到以下的WSDL文档了。
那么我们就会想知道,每一个方法的参数是什么,返回值是什么?接下来我们要看这个操作对应的输入消息是什么
我们拿sayHi这个操作来说:
因此我们需要找到消息的名字是sayHi的消息,即
我们详细的解析一下这个:
消息传出
<getCatsByUserResponse>
getCatsByUserResponse>
下面是简单的wsdl文档结构图: