本篇内容介绍了“Java WebService开源框架CXF案例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
CXF是一个开源的WebService框架。Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。
支持 JAX-WS、 JAX-WSA、JSR-181 和 SAAJ;
支持 SOAP 1.1、1.2、WS-I BasicProfile、WS-Security、WS-Addressing、WS-RM 和 WS-Policy;
支持 WSDL 1.1 、2.0;
支持 MTOM;
它支持多种协议,比如:SOAP1.1,1,2、XML/HTTP、RESTful HTTP 或者 CORBA。CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#)
Cxf是基于SOA总线结构,依靠spring完成模块的集成,实现SOA方式。
灵活的部署:可以运行有Tomcat,Jboss,Jetty(内置),weblogic上面。
我们还以昨天的天气服务为案例来看一下CXF的开发过程。
1.创建一个空的java项目,创建一个lib目录,将所有jar包放入lib目录
然后为工程引入jar包,选择build path,然后Add JARS,只用选择cxf-manifest.jar即可。
2.创建一个SEI接口,需要在接口上添加@WebService注解
@WebService
public interface WeatherInterface {
public String queryWeather(String cityName);
}
3.创建SEI接口实现类
public class WeatherInterfaceImpl implements WeatherInterface {
public String queryWeather(String cityName) {
if("河南".equals(cityName)) {
return "热爆炸";
}else {
return "冰雹";
}
}
}
4.发布服务
public class WeatherServer {
public static void main(String[] args) {
//创建服务工厂Bean
JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
//设置服务接口
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//设置服务实现类
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
//设置服务地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//创建服务
jaxWsServerFactoryBean.create();
}
}
5.访问服务的wsdl文件地址,看服务是否发布成功 http://127.0.0.1:12345/weather?wsdl
发布SOAP1.2的服务端
SOAP分为1.1版本和1.2版本。JDK1.6并不支持1.2,我们可以通过CXF来发布SOAP1.2的服务端。
只需要在接口上添加注解 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)。然后重新发布服务即可
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
Wsdl2java命令是CXF提供的生成客户端的工具,他和wsimport类似,可以根据WSDL生成客户端代码
Wsdl2java常用参数:
-d,指定输出目录
-p,指定包名,如果不指定该参数,默认包名是WSDL的命名空间的倒序
Wsdl2java支持SOAP1.1和SOAP1.2
1.我们先创建一个客户端项目,然后引入jar包,和上面一样,使用Add JARS选择cxf-manifest.jar即可 然后使用工具生成客户端 wsdl2java -p com.cad.cxf -d . http://127.0.0.1:12345/weather?wsdl
2.创建客户端
public class WeatherClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
//设置服务接口
jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
//设置服务地址
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//获取服务接口实例
WeatherInterface weatherInterface=(WeatherInterface) jaxWsProxyFactoryBean.create();
//调用方法
String message=weatherInterface.queryWeather("河南");
System.out.println(message);
}
}
1.创建WEB项目,导入jar包
2.创建SEI接口
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
3.创建SEI实现类
public class WeatherInterfaceImpl implements WeatherInterface {
public String queryWeather(String cityName) {
if("河南".equals(cityName)) {
return "热爆炸";
}else {
return "冰雹";
}
}
}
4.配置spring配置文件,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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!--jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
<!--serviceClass属性是服务接口,address代表地址,因为我们是web服务,不需要输入ip。serviceBean是服务实现类-->
<jaxws:server serviceClass="com.cad.cxf.WeatherInterface" address="/weather">
<jaxws:serviceBean>
<ref bean="weatherInterfaceImpl"/>
</jaxws:serviceBean>
</jaxws:server>
<bean name="weatherInterfaceImpl" class="com.cad.cxf.WeatherInterfaceImpl"></bean>
</beans>
5.配置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>CXFSpringDemo</display-name>
//配置Tomcat启动时加载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>
//配置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>
<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>
</web-app>
6.部署到Tomcat下,发布服务,并访问 http://localhost:8080/CXFSpringDemo/ws/weather?wsdl
1.创建项目,导入jar包,生成客户端 wsdl2java -p com.cad.cxf -d . http://localhost:8080/CXFSpringDemo/ws/weather?wsdl
2.配置Spring文件
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- <jaxws:client实现客户端 ,对JaxWsProxyFactoryBean类封装-->
<!-- address是服务地址,servicelass是服务接口,返回服务实现类-->
<jaxws:client id="weatherClient" address="http://localhost:8080/CXFSpringDemo/ws/weather" serviceClass="com.cad.cxf.WeatherInterface"/>
</beans>
3.通过Spring容器获取服务实现类,调用方法
public class WeatherClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
WeatherInterface weatherInterface = (WeatherInterface) context.getBean("weatherClient");
String message=weatherInterface.queryWeather("河南");
System.out.println(message);
}
}
REST即表述性状态传递(英文:Representational State Transfer,简称REST),是一种软件架构风格。
因为REST模式的Web服务与复杂的SOAP和XML-RPC对比来讲明显的更加简洁,越来越多的web服务开始采用REST风格设计和实现rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位。
例如:
非rest方式:http://ip:port/queryUser.action?userType=student&id=001
Rest方式:http://ip:port/user/student/query/001
1.创建一个项目,导入CXF jar包
2.创建一个实体类 Student
@XmlRootElement(name="student")可以实现XML和对象之间的转换,name属性指定根元素
@XmlRootElement(name="student")
public class Student {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3.创建SEI接口
@WebService
//@Path("/student")就是指定访问该接口的路径
@Path("/Student")
public interface StudentInterface {
//指定请求方式,如果服务端发布的时候指定的是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);
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/queryList/{name}")
public List<Student> queryList(@PathParam("name")String name);
}
4.创建SEI实现类
public class StudentInterfaceImpl implements StudentInterface {
@Override
public Student queryStudent(int id) {
Student s=new Student();
s.setId(666);
s.setName("张三");
s.setBirthday(new Date());
return s;
}
@Override
public List<Student> queryList(String name) {
Student s=new Student();
s.setId(666);
s.setName("张三");
s.setBirthday(new Date());
Student s2=new Student();
s2.setId(888);
s2.setName("李四");
s2.setBirthday(new Date());
List<Student> l=new ArrayList<Student>();
l.add(s);
l.add(s2);
return l;
}
}
5.发布服务
public class StudentServer {
public static void main(String[] args) {
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();
}
}
6.测试服务
访问query方法
http://127.0.0.1:12345/Class/Student/query/001
访问queryList方法 http://127.0.0.1:12345/Class/Student/queryList/xxx
如果服务端发布时指定请求方式是GET(POST),客户端必须使用GET(POST)访问服务端,否则会报异常。
如果在同一方法上同时指定XML和JSON媒体类型,在GET请求下,默认返回XML,在POST请求下,默认返回JSON
CXF+Spring整合发布REST模式的服务
综合案例:手机归属地查询
“Java WebService开源框架CXF案例分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。