JAX-WS实现Web Services开发

接口:
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());
    }  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值