一、需求
- 调用webService服务,把用户名和密码封装为用户对象作为参数,返回该用户所用友的角色列表。
二、发布服务
2.1 编写服务接口
@WebService public interface ILogin { @WebResult(name="getRoleByUserResult") public List<Role> getRoleByUser(@WebParam(name="user")User user); }
2.2 实现服务
package com.shyroke.service; import java.util.ArrayList; import java.util.List; import javax.jws.WebService; import com.shyroke.bean.Role; import com.shyroke.bean.User; @WebService(endpointInterface="com.shyroke.service.ILogin") public class Login implements ILogin { public List<Role> getRoleByUser(User user) { List<Role> roleList = new ArrayList<Role>(); if (user.getUserName().equals("admin") && user.getPassWord().equals("123")) { roleList.add(new Role(1, "管理员")); roleList.add(new Role(2, "普通用户")); } else if (user.getUserName().equals("user") && user.getPassWord().equals("123")) { roleList.add(new Role(2, "普通用户")); } else { return null; } return roleList; } }
2.3 发布服务
package com.shyroke.publish; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.shyroke.service.ILogin; import com.shyroke.service.Login; public class PublishMain { public static void main(String[] args) { String address="http://localhost:3333/login"; JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean(); factoryBean.setAddress(address); factoryBean.setServiceClass(ILogin.class); factoryBean.setServiceBean(new Login()); factoryBean.create(); System.out.println("服务发布......."); } }
2.4 结果
三、客户端调用
3.1 根据wsdl文件生成client
- 命令的含义请参考上一章节
3.2 测试
package com.shyroke.service; import java.util.List; public class Client { public static void main(String[] args) { User user=new User(); user.setId(1); user.setUserName("admin"); user.setPassWord("123"); ILogin login=new ILoginService().getILoginPort(); List<Role> roleList=login.getRoleByUser(user); for(Role role:roleList) { System.out.println(role.getId()+" "+role.getRoleName()); } } }