webservice框架,Apache CXF
1.CXF
下载包
环境配置变量
1. 找到CXF的安装目录,复制路径:D:\apache-cxf-2.5.9\bin(这是我本地路径);
2. 把D:\apache-cxf-2.5.9\bin加入到环境变量的系统变量的path中即可。
命令行输入 wsdl2java看效果
开发服务端 基于工厂的方式
I
package webservice_cxf_server;
import javax.jws.WebService;
@WebService
public interface IWeatherService {
public String query(String cityName);
}
Impl
package webservice_cxf_server;
public class WeatherServiceImpl implements IWeatherService {
@Override
public String query(String cityName) {
System.out.println("查询"+cityName);
return "有你,便是晴天";
}
}
-----------------------------------------------------------
Client.java
package webservice_cxf_server.client;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import webservice_cxf_server.IWeatherService;
import webservice_cxf_server.WeatherServiceImpl;
public class Client {
public static void main(String[] args)
{
JaxWsServerFactoryBean factoryBean=new JaxWsServerFactoryBean();
//1.设置发布的地址
factoryBean.setAddress("http://localhost:8888/weatherService");
//2.设置服务的接口
factoryBean.setServiceClass(IWeatherService.class);
//3.设置服务的实现对象
factoryBean.setServiceBean(new WeatherServiceImpl());
//4.通过工厂创建服务
factoryBean.create();
System.out.println("发布服务成功!20200421");
}
}
开发客户端
cmd在src目录生成地址
wsdl2java -d . http://localhost:8888/weatherService?wsdl
package webservice_cxf_server.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import webservice_cxf_server.IWeatherService;
public class Client {
public static void main(String[] args)
{
JaxWsProxyFactoryBean proxyFactoryBean =new JaxWsProxyFactoryBean();
//1.设置服务的地址
proxyFactoryBean.setAddress("http://localhost:8888/weatherService");
//2.设置服务的端口
proxyFactoryBean.setServiceClass(IWeatherService.class);
//3.通过工厂获取对象
IWeatherService weatherService =(IWeatherService)proxyFactoryBean.create();
String query=weatherService.query("大湖南");
System.out.println(query);
}
}
2.Spring整合CXF
就是改为spring配置的方式:
两个标签的使用;
1.创建web工程,引入cxf的jar包
2.创建SEI接口**。(SEI(WenService EndPoint Interface))---服务端用来处理请求的接口
3.接口实现类
3.SSM的项目
如何在系统内部如何调用第三方服务
-----------------------------------------------------------------------------------------------------
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.qf.translate.controller"/>
<!--配置视图渲染器-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
<!-- /view.jsp -->
</bean>
<!--配置静态资源映射-->
<mvc:resources location="/js/" mapping="/js/*"></mvc:resources>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>webservice_cxf_spring_server</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 1.启动Spring容器 -->
<!-- 配置Spring环境 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2.SpringMVC的前端控制器 -->
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 2.创建CXF提供的Servlet,来处理关于服务的请求 -->
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 扫描service -->
<context:component-scan base-package="com.qf.translate.service"></context:component-scan>
<!-- 作为客户端,调用网络上的翻译服务-->
<!-- 生成客户端代码 -->
<jaxws:client
id="translateClient"
serviceClass="cn.com.webxml.EnglishChineseSoap"
address="http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl">
</jaxws:client>
</beans>
-----------------------------------------------------------------------------------------------------------
4.
SSM服务端,发布服务
1.开发基本的查询功能和添加功能
2.将这些功能以服务的方式发布出来
放弃了!!!!!
实现SSM
MyBatis
SSM客户端,调用服务
访问前面的webservice_ssm_server发布的服务
放弃了!!!!