Web Service
1、定义: 跨平台,跨语言的远程调用技术。web service接口实际上就是RPC(远程过程调用)的一种实现。
RPC不仅可以走HTTP/HTTPS, 也可以自定义 tcp 协议, 从而省略HTTP繁杂的规则和冗余信息。
2、如何使用?
(1)编写webservice接口
查看代码
#定义接口
@WebService
public interface UserWebService {
}
#定义接口实现并main手动发布
@WebService
public class MyUserWebServiceImpl implements MyUserWebService {
@Autowired
private Student student;
//static方法不会被发布
public static String sayHello1(String name){
return "hello1,"+name;
}
//final方法不会被发布
public final String sayHello2(String name){
return "hello2,"+name;
}
//@WebMethod(exclude=true)排除后,方法不会被发布
@WebMethod(exclude=true)
public String sayHello3(String name){
return "hello3,"+name;
}
public String sayHello4(String name){
return "hello4,"+name;
}
@Override
@WebMethod
public UserResult sayHello(String name) {
UserResult userResult = new UserResult();
userResult.setMessage("hello,"+name);
return userResult;
}
@Override
@WebMethod
public UserResult getUsers() {
UserResult userResult = new UserResult();
UserDTO userDTO = new UserDTO();
userDTO.setName(student.getName());
userDTO.setAge(student.getAge());
userResult.setUserDto(userDTO);
return userResult;
}
public static void main(String[] args) {
MyUserWebService webService = new MyUserWebServiceImpl();
String address = "http://localhost:8080/MyUserWebServiceImpl";
Endpoint.publish(address, webService);
System.out.println("发布成功,请访问:http://localhost:8080/MyUserWebServiceImpl?wsdl");
}
}
#定义vo类
public class UserDTO {
}
#定义返回对象
public class UserResult {
/**
* 成功
*/
public static final String SUCCESS = "0000";
/**
* 失败
*/
public static final String FAILED = "0001";
/**
* 缺少参数
*/
public static final String MISSPARAMETER = "0002";
/**
* 参数异常
*/
public static final String PARAMETERERROR = "0003";
/**
* /无权限
*/
public static final String AUTHORITYERROR="0004";
/**
* 请求数据为空
*/
public static final String NULLERROR="0005";
/**
* 重复
*/
public static final String REPEAT="0006";
/**
* 用户DTO
*/
private UserDTO userDto;
private String ResultCode = SUCCESS;
private String Message = "";
public UserResult buildResult(String resultCode, String resultMessage) {
setResult(resultCode, resultMessage);
return this;
}
/**
* 设置返回结果.
*/
public void setResult(String resultCode, String resultMessage) {
ResultCode = resultCode;
Message = resultMessage;
}
public UserDTO getUserDto() {
return userDto;
}
public void setUserDto(UserDTO userDto) {
this.userDto = userDto;
}
public String getResultCode() {
return ResultCode;
}
public void setResultCode(String resultCode) {
ResultCode = resultCode;
}
public String getMessage() {
return Message;
}
public void setMessage(String message) {
Message = message;
}
public String toString(){
JSONObject objectInput = new JSONObject();
objectInput.put("UserResult", this);
return objectInput.toString();
}
}
(2)服务端Server发布接口
注意:
- 类上添加注解@WebService,类中所有非静态方法都会被发布
- 静态方法和final方法不能被发布
- 方法上加@WebMentod(exclude=true)后,此方法不被发布
借助jdk的wsimort.exe工具生成客户端代码:wsimport -keep http://localhost:8989/webservice/hello?wsdl,wsimport -keep url,其中url为wsdl文件的路径。
wsimport -s . http://localhost:8082/MyUserWebServiceImpl?wsdl
(3)Client客户端吧代码复制到本地,再调用接口
#删除接口的注解
@XmlSeeAlso({
ObjectFactory.class
})
dto
result
service
serviceImpl复制到本地代码
#该import的import
public static void main(String[] args) {
// 创建服务视图,视图是从wsdl文件的service标签的name属性获取
MyUserWebServiceImplService factory = new MyUserWebServiceImplService();
// 获取到jdk动态对立对象
// 获取服务实现类,实现类从wsdl文件的portType的name属性获取
MyUserWebServiceImpl helloWebServiceSEIImplPort = factory.getMyUserWebServiceImplPort();
// class com.sun.proxy.$Proxy32
System.out.println(helloWebServiceSEIImplPort.getClass());
// 获取查询方法,从portType的operation标签获取
String sayHello4 = helloWebServiceSEIImplPort.sayHello4("hell04");
System.out.println(sayHello4);
UserResult result4 = helloWebServiceSEIImplPort.sayHello("hell04");
System.out.println(result4.getMessage());
UserResult result = helloWebServiceSEIImplPort.getUsers();
System.out.println(result.getUserDto().toString());
}