创建服务器端
1.创建一个接口
IMyService.java
package com.test.service;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService(targetNamespace = "http://www.webservice.com")
public interface IMyService {
// 异常处理
@WebResult(name = "user")
public User login(@WebParam(name = "username") String username, @WebParam(name = "password") String password) throws UserException;
}
2.创建实现接口的类
MyServiceImpl.java
package com.test.service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
@WebService(endpointInterface = "com.test.service.IMyService", targetNamespace = "http://www.webservice.com")
public class MyServiceImpl implements IMyService {
private static List<User> users = new ArrayList<User>();
public MyServiceImpl() {
users.add(new User(1, "admin", "管理员", "111111"));
}
@Override
public User login(String username, String password) throws UserException {
for (User user : users) {
if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
return user;
}
}
throw new UserException("用户名或密码不正确!");
}
}
3.自定义异常类
UserException.java
package com.test.service;
public class UserException extends Exception {
public UserException() {
super();
}
public UserException(String message, Throwable cause) {
super(message, cause);
}
public UserException(String message) {
super(message);
}
public UserException(Throwable cause) {
super(cause);
}
}
4.开启服务
MyServer.java
package com.test.service;
import javax.xml.ws.Endpoint;
public class MyServer {
public static void main(String[] args) {
String address = "http://localhost:9999/server";
// 发布服务的实现类
Endpoint.publish(address, new MyServiceImpl());
}
}
5.测试类TestSoap.java
// 异常处理
@Test
public void test05() {
try {
// 创建服务
URL url = new URL("http://localhost:9999/server");
QName qname = new QName("http://www.webservice.com", "MyServiceImplService");
Service service = Service.create(url, qname);
// 创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://www.webservice.com", "MyServiceImplPort"), SOAPMessage.class, Service.Mode.MESSAGE);
// 创建SOAP的body消息
SOAPMessage message = MessageFactory.newInstance().createMessage();
SOAPBody body = message.getSOAPPart().getEnvelope().getBody();
// 根据QName创建相应的节点
QName ename = new QName("http://www.webservice.com", "login", "ns");
SOAPBodyElement element = body.addBodyElement(ename);
element.addChildElement("username").setValue("cdxs");
element.addChildElement("password").setValue("123");
message.writeTo(System.out);
System.out.println("\n invoking...");
// 通过dispatch传递消息,返回响应消息
SOAPMessage response = dispatch.invoke(message);
response.writeTo(System.out);
System.out.println();
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SOAPFaultException e) {
System.out.println(e.getMessage());
}
}
运行结果:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns:login xmlns:ns="http://www.webservice.com"><username>cdxs</username><password>123</password></ns:login></SOAP-ENV:Body></SOAP-ENV:Envelope>
invoking...
用户名或密码不正确!