自定义webService
Posted popcorn丫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义webService相关的知识,希望对你有一定的参考价值。
要生成一个wsdl,首先要有一个ws,建立一个简单的ws
package com.bxw.server; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.ws.Endpoint; /* * 自定义ws服务, jdk1.6版本仅仅支持 soap1.1格式,jdk1.7及以上版本支持 soap1.2格式 * 发布ws服务只需要@WebService注解即可, 如果想要更好的可维护性,则可以通过注解来实现 * */ @WebService // 默认静态的方式是不能发布ws服务的 ( name="MyWebService1", // 服务实现类的名称 serviceName="MyWebServiceService1", // 默认在发布的服务实现者的名称后面添加Service portName="MyWebServicePort1", // 服务类型的名称: 默认在 发布的服务实现者(MyWebService) 后面添加 port targetNamespace="com.bxw.ws" // 发布ws服务的命名空间,此空间默认为当前服务包路径的 "倒写"此名称也是 wsimport 命令生成 java类时默认的包路径 -p ) public class WebService1 { @WebMethod(exclude=true) // 默认public方法可以发布为ws服务, 如果要排除则配置 exclude=true public String saySth(String text){ return "say"+text; } //可以指定wsdl中的方法名,参数名和返回值 @WebMethod(operationName="saySth") public @WebResult(name="result") String saySth(@WebParam(name="text") String text,@WebParam(name="age") String age){ return "say "+text+"|| age "+age; } public static void main(String[] args) { String address = "http://localhost:9090/ws"; Endpoint.publish(address, new WebService1()); System.out.println("访问wsdl的地址:"+address+"?WSDL"); } }
要创建一个ws,只需要添加@webService注解即可。其余注解用法已写在注释中。运行后,访问服务发布的地址:http://localhost:9090/ws?wsdl
之后创建客户端:
cmd运行wsimport命令后,将生成的java文件copy至客户端项目中。
package com.bxw.client; import java.net.MalformedURLException; import java.net.URL; import com.bxw.ws.MyWebService1; import com.bxw.ws.MyWebServiceService1; public class WebClient { public static void main(String[] args) throws MalformedURLException { URL url = new URL("http://localhost:9090/ws?WSDL"); MyWebServiceService1 service = new MyWebServiceService1(url); MyWebService1 ss = service.getMyWebServicePort1(); System.out.println(ss.saySth("a", "1")); } }
url可以在配置文件中配置,以防路径改变。
================================分割线========================================
cxf中使用map:
实体:
package com.bxw.entity; public class User { private String id; private String username; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
package com.bxw.entity; public class Role { private String id; private String roleName; public Role() { } public Role(String id,String roleName){ this.id = id; this.roleName = roleName; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } }
service:
package com.bxw.service; import java.util.List; import java.util.Map; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.bxw.entity.MapAdapter; import com.bxw.entity.Role; import com.bxw.entity.User; @WebService public interface Service { public List<Role> getRole(User user); @XmlJavaTypeAdapter(MapAdapter.class) public Map<String, List<Role>> getRoles(); }
注意@XmlJavaTypeAdapter(MapAdapter.class)这里返回的map类型需要自己编写一个转换器转换。
mapAdapter:
package com.bxw.entity; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; /** * Map适配器 * @author Administrator * */ public class MapAdapter extends XmlAdapter<MyRole[], Map<String,List<Role>>>{ /** * 适配转换 MyRole[] -> Map<String, List<Role>> */ @Override public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception { Map<String, List<Role>> map=new HashMap<String,List<Role>>(); for(int i=0;i<v.length;i++){ MyRole r=v[i]; map.put(r.getKey(), r.getValue()); } return map; } /** * 适配转换 Map<String, List<Role>> -> MyRole[] */ @Override public MyRole[] marshal(Map<String, List<Role>> v) throws Exception { MyRole[] roles=new MyRole[v.size()]; int i=0; for(String key:v.keySet()){ roles[i]=new MyRole(); roles[i].setKey(key); roles[i].setValue(v.get(key)); i++; } return roles; } }
将map转换为自定义的MyRole数组类型:
MyRole:
package com.bxw.entity; import java.util.List; /** * 自定义实体 cxf能接受 * @author Administrator * */ public class MyRole { private String key; private List<Role> value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public List<Role> getValue() { return value; } public void setValue(List<Role> value) { this.value = value; } }
impl:
package com.bxw.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import com.bxw.entity.Role; import com.bxw.entity.User; import com.bxw.service.Service; @WebService public class ServiceImpl implements Service{ public List<Role> getRole(User user) { List<Role> ls = new ArrayList<Role>(); ls.add(new Role(user.getId(), "admin")); return ls; } public Map<String, List<Role>> getRoles() { Map<String, List<Role>> map = new HashMap<String, List<Role>>(); List<Role> ls = new ArrayList<Role>(); ls.add(new Role("1","admin")); ls.add(new Role("2","customer")); map.put("key", ls); return map; } }
Test:
package com.bxw.test; import javax.xml.ws.Endpoint; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.bxw.service.Service; import com.bxw.service.impl.ServiceImpl; public class Test { public static void main(String[] args) { System.out.println("web service start"); Service implementor = new ServiceImpl(); String address = "http://localhost:9095/helloWorld"; Endpoint.publish(address, implementor); // JDK实现 // JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); // factoryBean.setAddress(address); // 设置暴露地址 // factoryBean.setServiceClass(Service.class); // 接口类 // factoryBean.setServiceBean(implementor); // 设置实现类 // factoryBean.create(); } }
以上是关于自定义webService的主要内容,如果未能解决你的问题,请参考以下文章