WebService的理解与使用

WebService的理解与使用

本文章只是做为个人学习归纳的笔记,感谢春水上行的两篇文章
https://blog.csdn.net/c99463904/article/details/76018436
https://blog.csdn.net/c99463904/article/details/76140591

重点归纳:
1) Endpoint类publish方法发布服务
Endpoint.publish(“http://127.0.0.1:12345/weather”, new WeatherInterfaceImpl());

CXF的JaxWsServerFactoryBean发布
//创建服务工厂Bean
JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
//设置服务接口
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//设置服务实现类
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
//设置服务地址
jaxWsServerFactoryBean.setAddress(“http://127.0.0.1:8080/weather”);
//创建服务
jaxWsServerFactoryBean.create();
2) 生成客户端代码wsimport和wsdl2java
wsimport.exe位于JAVA_HOME\bin目录下
3) Webservice的客户端调用方式
a. wsimport生成客户端代码
//创建服务视图
MobileCodeWS mobileCodeWS=new MobileCodeWS();
//获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap= mobileCodeWS.getPort(MobileCodeWSSoap.class);
//调用查询方法 String message=mobileCodeWSSoap.getMobileCodeInfo(“xxxxxxxx”, null);
b. service编程调用方式
//创建WSDL文件的URL
URL wsdlDocumentLocation=new URL(“http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl”); //创建服务名称
//1.namespaceURI - 命名空间地址
//2.localPart - 服务视图名
QName serviceName=new QName(“http://WebXml.com.cn/”,“MobileCodeWS”); Service service=Service.create(wsdlDocumentLocation, serviceName); //获取服务实现类
MobileCodeWSSoap mobileCodeWSSoap= service.getPort(MobileCodeWSSoap.class);
//调用方法 String message=mobileCodeWSSoap.getMobileCodeInfo(“XXXXXXX”, null);
c. HttpURLConnection调用方式
要自己开发SOAP报文数据,比较麻烦
开发步骤:
第一步:创建服务地址
第二步:打开一个通向服务地址的连接
第三步:设置参数
第四步:组织SOAP数据,发送请求
第五步:接收服务端响应
4) WSDL内容可通过注解修改
5) CXF客户端实现,代码生成工具使用wsdl2java
使用该工具命令需要配置环境变量path:D:\临时下载\apache-cxf-3.4.2\apache-cxf-3.4.2\bin
6) CXF+Spring整合发布SOAP模式的服务
服务端:applicationContext.xml

web.xml
//配置Tomcat启动时加载Spring配置文件
contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener
//配置CXF提供的Servlet
CXF org.apache.cxf.transport.servlet.CXFServlet CXF /ws/*

客户端的实现
a. wsdl2java生成代码
b. applicationContext.xml

c. 调用方法实现类(可以不用tomcat容器,直接run)
ApplicationContext context = new ClassPathXmlApplicationContext(“classpath:applicationContext.xml”); WeatherInterface weatherInterface = (WeatherInterface) context.getBean(“weatherClient”); String message=weatherInterface.queryWeather(“河南”); System.out.println(message);
7) CXF发布REST模式的服务
a. 接口类实现rest
@WebService
//@Path("/student")就是指定访问该接口的路径
@Path("/Student")
public interfaceStudentInterface {
//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST
@GET
//指定服务数据类型,可以是XML,json等数据类型 @Produces(MediaType.APPLICATION_XML)
//@Path("/query/{id}")指定该方法的路径,“{id}”指参数,多个参数,以“/”隔开,放到“{}”中
@Path("/query/{id}")
public Student queryStudent(@PathParam(“id”)int id);
}

b. 发布服务
JAXRSServerFactoryBean jaxrsServerFactoryBean=new JAXRSServerFactoryBean();
//设置服务实现类
jaxrsServerFactoryBean.setServiceBean(new StudentInterfaceImpl()); //设置资源类,如果有多个资源类,可以以“,”隔开,例如Student.class StudentInterface.class都是资源类,但是StudentInterfaceImpl里面已经包含了Student.class StudentInterface.class,所以不用重复指定 jaxrsServerFactoryBean.setResourceClasses(StudentInterfaceImpl.class); //设置服务地址 jaxrsServerFactoryBean.setAddress(“http://127.0.0.1:12345/Class”); //发布服务
jaxrsServerFactoryBean.create();
c. 测试服务
http://127.0.0.1:12345/Class/Student/query/001
8) CXF+Spring整合发布REST模式的服务
跟7)的区别就是增加spring引入
applicationContext.xml

web.xml

contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener CXF org.apache.cxf.transport.servlet.CXFServlet CXF /ws/*

注意:
在部署项目的过程中,特别是CXF,遇到很多问题,常见的是各类jar包的问题,记录一下。
在没有结合spring使用时,自己使用tomcat8+JDK1.8+apache官网最新下载的CXF包
在这里插入图片描述

使用spring时由于自己已有一些包的版本情况,tomcat用的7,JDK1.7+自己导入的相关包
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值