接口:
package com.chj.web.service;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.chj.web.entity.User;
@WebService
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface HelloWorldService {
@WebMethod
public String sayHello(String name);
@WebMethod
public List<String> getList(Integer index);
@WebMethod
public String getListToString(List<String> list);
@WebMethod
public User getUser();
}
实现类:
package com.chj.web.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.chj.web.entity.User;
import com.chj.web.service.HelloWorldService;
@WebService(endpointInterface = "com.chj.web.service.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService{
@Override
public String sayHello(String name) {
return "Hello " + name + "!";
}
@Override
public List<String> getList(Integer index) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < index; i++) {
list.add("list - " + i);
}
return list;
}
@Override
public String getListToString(List<String> list) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
if(i == list.size() - 1){
sb.append(list.get(i));
}else{
sb.append(list.get(i)).append(" , ");
}
}
return sb.toString();
}
@Override
public User getUser() {
User user = new User();
user.setUsername("ABC");
user.setPassword("abcdefg");
user.setSex("MAN");
user.setAgs(20);
List<String> list = getList(5);
user.setList(list);
return user;
}
}
服务发布类Publisher
package com.chj.web;
import javax.xml.ws.Endpoint;
import com.chj.web.service.impl.HelloWorldServiceImpl;
public class Publisher {
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1/chj-web", new HelloWorldServiceImpl());
}
}
运行Publisher发布服务后在浏览器地址栏输入:http://localhost/chj-web?wsdl 会显示
<definitions targetNamespace="http://impl.service.web.chj.com/"
name="HelloWorldServiceImplService">
<import namespace="http://service.web.chj.com/" location="http://localhost/chj-web?wsdl=1" />
<binding name="HelloWorldServiceImplPortBinding" type="ns1:HelloWorldService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="sayHello">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="getList">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="getListToString">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="getUser">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="HelloWorldServiceImplService">
<port name="HelloWorldServiceImplPort" binding="tns:HelloWorldServiceImplPortBinding">
<soap:address location="http://localhost/chj-web" />
</port>
</service>
</definitions>
java客户端代码测试服务。
package com.chj.web;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.chj.web.entity.User;
import com.chj.web.service.HelloWorldService;
public class WSTest {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost/chj-web?wsdl");
// 第一个参数是服务的URI
// 第二个参数是在WSDL发布的服务名
QName qname = new QName("http://impl.service.web.chj.com/","HelloWorldServiceImplService");
// 创建服务
Service service = Service.create(url, qname);
// 提取端点接口,服务“端口”。
HelloWorldService hw = service.getPort(HelloWorldService.class);
System.out.println(hw.sayHello("ABC"));
List<String> list = hw.getList(10);
String result = hw.getListToString(list);
System.out.println(result);
User user = hw.getUser();
System.out.println(user.toString());
}
}
package com.chj.web.service;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import com.chj.web.entity.User;
@WebService
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface HelloWorldService {
@WebMethod
public String sayHello(String name);
@WebMethod
public List<String> getList(Integer index);
@WebMethod
public String getListToString(List<String> list);
@WebMethod
public User getUser();
}
实现类:
package com.chj.web.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import com.chj.web.entity.User;
import com.chj.web.service.HelloWorldService;
@WebService(endpointInterface = "com.chj.web.service.HelloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService{
@Override
public String sayHello(String name) {
return "Hello " + name + "!";
}
@Override
public List<String> getList(Integer index) {
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < index; i++) {
list.add("list - " + i);
}
return list;
}
@Override
public String getListToString(List<String> list) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
if(i == list.size() - 1){
sb.append(list.get(i));
}else{
sb.append(list.get(i)).append(" , ");
}
}
return sb.toString();
}
@Override
public User getUser() {
User user = new User();
user.setUsername("ABC");
user.setPassword("abcdefg");
user.setSex("MAN");
user.setAgs(20);
List<String> list = getList(5);
user.setList(list);
return user;
}
}
服务发布类Publisher
package com.chj.web;
import javax.xml.ws.Endpoint;
import com.chj.web.service.impl.HelloWorldServiceImpl;
public class Publisher {
public static void main(String[] args) {
Endpoint.publish("http://127.0.0.1/chj-web", new HelloWorldServiceImpl());
}
}
运行Publisher发布服务后在浏览器地址栏输入:http://localhost/chj-web?wsdl 会显示
<definitions targetNamespace="http://impl.service.web.chj.com/"
name="HelloWorldServiceImplService">
<import namespace="http://service.web.chj.com/" location="http://localhost/chj-web?wsdl=1" />
<binding name="HelloWorldServiceImplPortBinding" type="ns1:HelloWorldService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="sayHello">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="getList">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="getListToString">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
<operation name="getUser">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="HelloWorldServiceImplService">
<port name="HelloWorldServiceImplPort" binding="tns:HelloWorldServiceImplPortBinding">
<soap:address location="http://localhost/chj-web" />
</port>
</service>
</definitions>
java客户端代码测试服务。
package com.chj.web;
import java.net.URL;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.chj.web.entity.User;
import com.chj.web.service.HelloWorldService;
public class WSTest {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost/chj-web?wsdl");
// 第一个参数是服务的URI
// 第二个参数是在WSDL发布的服务名
QName qname = new QName("http://impl.service.web.chj.com/","HelloWorldServiceImplService");
// 创建服务
Service service = Service.create(url, qname);
// 提取端点接口,服务“端口”。
HelloWorldService hw = service.getPort(HelloWorldService.class);
System.out.println(hw.sayHello("ABC"));
List<String> list = hw.getList(10);
String result = hw.getListToString(list);
System.out.println(result);
User user = hw.getUser();
System.out.println(user.toString());
}
}