当前内容主要为本人记录使用webservice使用携带header的请求方式
主要内容为:
- 创建携带参数调用的webservice方法创建携带header认证的方法使用soapui方式测试请求使用wsimport方式完成携带header验证的请求
创建实体类User
@XmlRootElement public class User { private String username; private Integer id; // 省略getset方法 }
创建响应实体类Response
public class Response { private Boolean status; private Object data; private String msg; // 省略无参有参getset方法 }
在webservice接口中添加一个携带参数的方法
@WebResult @WebMethod(action = "addUser") Response addUser(User user);
在webservice的实现类中实现该方法
@Override public Response addUser(User user) { // TODO Auto-generated method stub userDB.put(user.getId(), user); Response response = new Response(); response.setStatus(true); return response; }
此时默认传递的参数会自动转换为User
下列为使用soapui生成的请求模板:
这个arg0虽然不好看,但是不影响请求,可以修改如下
1. 在User前面添加如下
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "user", propOrder = { "username", "id" }) public class User { }
2.在方法前面添加如下
@WebResult @WebMethod(action = "addUser") Response addUser(@WebParam( name = "user") User user);
此时使用soapui的结果:
==创建需要认证的实体类:UserNameAndPassword ==
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "UserNameAndPassword", propOrder = { "username", "password" }) public class UserNameAndPassword { private String username; private String password; // 省略getset方法 }
在webservice接口中创建认证方法
@WebResult @WebMethod(action = "getUserById") Response getUserById( @WebParam(header = true, name = "UserNameAndPassword") UserNameAndPassword userNameAndPassword, @WebParam(name = "id") Integer id);
在webservice接口的实现类中实现该认证方法
@Override public Response getUserById(UserNameAndPassword userNameAndPassword, Integer id) { if(!validate(userNameAndPassword)) { return new Response(false,null,"登录失败"); } User user = userDB.get(id); return new Response(true,user,null); } private boolean validate(UserNameAndPassword userNameAndPassword) { if(userNameAndPassword==null) { return false; } String username = userNameAndPassword.getUsername(); if(!StringUtils.hasText(username)) { return false; } return "admin".equals(username); }
这里默认认为用户名为admin的就是登陆成功的,下面是soapui生成的请求模板
1.生成java文件
wsimport -keep http://localhost:8081/show?wsdl
2.将java文件导入到项目中
3.编写测试
public class Test { public static void main(String[] args) { MyServiceImplService myServiceImplService = new MyServiceImplService(); MyService myService = myServiceImplService.getMyServiceImplPort(); User user = new User(); user.setId(4); user.setUsername("test"); Response addUserResponse = myService.addUser(user); System.out.println("添加数据结果:"+addUserResponse.isStatus()); GetUserById getUserById = new GetUserById(); getUserById.setId(4); UserNameAndPassword userNameAndPassword = new UserNameAndPassword(); // //userNameAndPassword.setUsername("admin"); //userNameAndPassword.setPassword(""); GetUserByIdResponse response = myService.getUserById(getUserById, userNameAndPassword); Response getResponse =response.getReturn(); if(!getResponse.isStatus()) { System.out.println("获取数据失败:"+getResponse.getMsg()); }else { User userById = (User)getResponse.getData(); System.out.println(userById.getId()+":"+userById.getUsername()); } } }
1. 不符合要求的请求结果(未设置username为admin)
2. 符合要求的请求结果:
1. 携带Header请求认证webservice的方式如下:
@WebMethod(action = "getUserById") @WebResult(name = "getUserByIdResponse", targetNamespace = "http://service.demo.webservice.springboot.hy.com/", partName = "result") @SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) @Action(input = "getUserById", output = "http://service.demo.webservice.springboot.hy.com/MyService/getUserByIdResponse") public GetUserByIdResponse getUserById( @WebParam(name = "getUserById", targetNamespace = "http://service.demo.webservice.springboot.hy.com/", partName = "parameters") GetUserById parameters, @WebParam(name = "UserNameAndPassword", targetNamespace = "http://service.demo.webservice.springboot.hy.com/", header = true, partName = "UserNameAndPassword") UserNameAndPassword userNameAndPassword);
在需要header认证的地方添加一个对应的类型参数,然后添加注解 @WebParam,并设置header = true,然后设置partName为该类的@XmlType中的name属性一致
2. 可以在使用的时候使用soapui方式测试当前的webservice接口,查看需要的参数
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)