Java?WebService開源框架CXF詳解
CXF簡(jiǎn)介
CXF是一個(gè)開源的WebService框架。Apache CXF = Celtix + XFire,開始叫 Apache CeltiXfire,后來(lái)更名為 Apache CXF 了,以下簡(jiǎn)稱為 CXF。CXF 繼承了 Celtix 和 XFire 兩大開源項(xiàng)目的精華,提供了對(duì) JAX-WS 全面的支持,并且提供了多種 Binding 、DataBinding、Transport 以及各種 Format 的支持,并且可以根據(jù)實(shí)際項(xiàng)目的需要,采用代碼優(yōu)先(Code First)或者 WSDL 優(yōu)先(WSDL First)來(lái)輕松地實(shí)現(xiàn) Web Services 的發(fā)布和使用。
支持多種標(biāo)準(zhǔn)
- 支持 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;
它支持多種協(xié)議,比如:SOAP1.1,1,2、XML/HTTP、RESTful HTTP 或者 CORBA。CORBA(Common Object Request Broker Architecture公共對(duì)象請(qǐng)求代理體系結(jié)構(gòu),早期語(yǔ)言使用的WS。C,c++,C#)
Cxf是基于SOA總線結(jié)構(gòu),依靠spring完成模塊的集成,實(shí)現(xiàn)SOA方式。
靈活的部署:可以運(yùn)行有Tomcat,Jboss,Jetty(內(nèi)置),weblogic上面。
CXF入門案例
我們還以昨天的天氣服務(wù)為案例來(lái)看一下CXF的開發(fā)過(guò)程。
服務(wù)端的實(shí)現(xiàn)
1.創(chuàng)建一個(gè)空的java項(xiàng)目,創(chuàng)建一個(gè)lib目錄,將所有jar包放入lib目錄 然后為工程引入jar包,選擇build path,然后Add JARS,只用選擇cxf-manifest.jar即可。


2.創(chuàng)建一個(gè)SEI接口,需要在接口上添加@WebService注解
@WebService
public interface WeatherInterface {
public String queryWeather(String cityName);
}
3.創(chuàng)建SEI接口實(shí)現(xiàn)類
public class WeatherInterfaceImpl implements WeatherInterface {
public String queryWeather(String cityName) {
if("河南".equals(cityName)) {
return "熱爆炸";
}else {
return "冰雹";
}
}
}
4.發(fā)布服務(wù)
public class WeatherServer {
public static void main(String[] args) {
//創(chuàng)建服務(wù)工廠Bean
JaxWsServerFactoryBean jaxWsServerFactoryBean=new JaxWsServerFactoryBean();
//設(shè)置服務(wù)接口
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//設(shè)置服務(wù)實(shí)現(xiàn)類
jaxWsServerFactoryBean.setServiceBean(new WeatherInterfaceImpl());
//設(shè)置服務(wù)地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//創(chuàng)建服務(wù)
jaxWsServerFactoryBean.create();
}
}
5.訪問(wèn)服務(wù)的wsdl文件地址,看服務(wù)是否發(fā)布成功
http://127.0.0.1:12345/weather?wsdl

發(fā)布SOAP1.2的服務(wù)端
SOAP分為1.1版本和1.2版本。JDK1.6并不支持1.2,我們可以通過(guò)CXF來(lái)發(fā)布SOAP1.2的服務(wù)端。
只需要在接口上添加注解 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)。然后重新發(fā)布服務(wù)即可
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);
}
客戶端的實(shí)現(xiàn)
Wsdl2java命令是CXF提供的生成客戶端的工具,他和wsimport類似,可以根據(jù)WSDL生成客戶端代碼 Wsdl2java常用參數(shù): -d,指定輸出目錄 -p,指定包名,如果不指定該參數(shù),默認(rèn)包名是WSDL的命名空間的倒序 Wsdl2java支持SOAP1.1和SOAP1.2
1.我們先創(chuàng)建一個(gè)客戶端項(xiàng)目,然后引入jar包,和上面一樣,使用Add JARS選擇cxf-manifest.jar即可 然后使用工具生成客戶端 wsdl2java -p com.cad.cxf -d . http://127.0.0.1:12345/weather?wsdl

2.創(chuàng)建客戶端
public class WeatherClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean=new JaxWsProxyFactoryBean();
//設(shè)置服務(wù)接口
jaxWsProxyFactoryBean.setServiceClass(WeatherInterface.class);
//設(shè)置服務(wù)地址
jaxWsProxyFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//獲取服務(wù)接口實(shí)例
WeatherInterface weatherInterface=(WeatherInterface) jaxWsProxyFactoryBean.create();
//調(diào)用方法
String message=weatherInterface.queryWeather("河南");
System.out.println(message);
}
}
CXF+Spring整合發(fā)布SOAP模式的服務(wù)
服務(wù)端的實(shí)現(xiàn)
1.創(chuàng)建WEB項(xiàng)目,導(dǎo)入jar包
2.創(chuàng)建SEI接口
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
public String queryWeather(String cityName);
}
3.創(chuàng)建SEI實(shí)現(xiàn)類
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發(fā)布SOAP協(xié)議的服務(wù) ,對(duì)JaxWsServerFactoryBean類封裝-->
<!--serviceClass屬性是服務(wù)接口,address代表地址,因?yàn)槲覀兪莣eb服務(wù),不需要輸入ip。serviceBean是服務(wù)實(shí)現(xiàn)類-->
<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啟動(dòng)時(shí)加載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下,發(fā)布服務(wù),并訪問(wèn) http://localhost:8080/CXFSpringDemo/ws/weather?wsdl

客戶端的實(shí)現(xiàn)
1.創(chuàng)建項(xiàng)目,導(dǎo)入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實(shí)現(xiàn)客戶端 ,對(duì)JaxWsProxyFactoryBean類封裝-->
<!-- address是服務(wù)地址,servicelass是服務(wù)接口,返回服務(wù)實(shí)現(xiàn)類-->
<jaxws:client id="weatherClient" address="http://localhost:8080/CXFSpringDemo/ws/weather" serviceClass="com.cad.cxf.WeatherInterface"/>
</beans>
3.通過(guò)Spring容器獲取服務(wù)實(shí)現(xiàn)類,調(diào)用方法
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);
}
}

CXF發(fā)布REST模式的服務(wù)
REST即表述性狀態(tài)傳遞(英文:Representational State Transfer,簡(jiǎn)稱REST),是一種軟件架構(gòu)風(fēng)格。
因?yàn)镽EST模式的Web服務(wù)與復(fù)雜的SOAP和XML-RPC對(duì)比來(lái)講明顯的更加簡(jiǎn)潔,越來(lái)越多的web服務(wù)開始采用REST風(fēng)格設(shè)計(jì)和實(shí)現(xiàn)rest服務(wù)采用HTTP 做傳輸協(xié)議,REST 對(duì)于HTTP 的利用實(shí)現(xiàn)精確的資源定位。
例如: 非rest方式:http://ip:port/queryUser.action?userType=student&id=001 Rest方式:http://ip:port/user/student/query/001
1.創(chuàng)建一個(gè)項(xiàng)目,導(dǎo)入CXF jar包
2.創(chuàng)建一個(gè)實(shí)體類 Student
@XmlRootElement(name="student")可以實(shí)現(xiàn)XML和對(duì)象之間的轉(zhuǎn)換,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.創(chuàng)建SEI接口
@WebService
//@Path("/student")就是指定訪問(wèn)該接口的路徑
@Path("/Student")
public interface StudentInterface {
//指定請(qǐng)求方式,如果服務(wù)端發(fā)布的時(shí)候指定的是GET(POST),那么客戶端訪問(wèn)時(shí)必須使用GET(POST
@GET
//指定服務(wù)數(shù)據(jù)類型,可以是XML,json等數(shù)據(jù)類型
@Produces(MediaType.APPLICATION_XML)
//@Path("/query/{id}")指定該方法的路徑,“{id}”指參數(shù),多個(gè)參數(shù),以“/”隔開,放到“{}”中
@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.創(chuàng)建SEI實(shí)現(xiàn)類
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.發(fā)布服務(wù)
public class StudentServer {
public static void main(String[] args) {
JAXRSServerFactoryBean jaxrsServerFactoryBean=new JAXRSServerFactoryBean();
//設(shè)置服務(wù)實(shí)現(xiàn)類
jaxrsServerFactoryBean.setServiceBean(new StudentInterfaceImpl());
//設(shè)置資源類,如果有多個(gè)資源類,可以以“,”隔開,例如Student.class StudentInterface.class都是資源類,但是StudentInterfaceImpl里面已經(jīng)包含了Student.class StudentInterface.class,所以不用重復(fù)指定
jaxrsServerFactoryBean.setResourceClasses(StudentInterfaceImpl.class);
//設(shè)置服務(wù)地址
jaxrsServerFactoryBean.setAddress("http://127.0.0.1:12345/Class");
//發(fā)布服務(wù)
jaxrsServerFactoryBean.create();
}
}
6.測(cè)試服務(wù)
訪問(wèn)query方法
http://127.0.0.1:12345/Class/Student/query/001

訪問(wèn)queryList方法
http://127.0.0.1:12345/Class/Student/queryList/xxx

如果服務(wù)端發(fā)布時(shí)指定請(qǐng)求方式是GET(POST),客戶端必須使用GET(POST)訪問(wèn)服務(wù)端,否則會(huì)報(bào)異常。
如果在同一方法上同時(shí)指定XML和JSON媒體類型,在GET請(qǐng)求下,默認(rèn)返回XML,在POST請(qǐng)求下,默認(rèn)返回JSON
CXF+Spring整合發(fā)布REST模式的服務(wù)
1.創(chuàng)建web項(xiàng)目,引入jar包
2.創(chuàng)建一個(gè)實(shí)體類 Student
@XmlRootElement(name="student")可以實(shí)現(xiàn)XML和對(duì)象之間的轉(zhuǎn)換,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.創(chuàng)建SEI接口
@WebService
//@Path("/student")就是指定訪問(wèn)該接口的路徑
@Path("/Student")
public interface StudentInterface {
//指定請(qǐng)求方式,如果服務(wù)端發(fā)布的時(shí)候指定的是GET(POST),那么客戶端訪問(wèn)時(shí)必須使用GET(POST
@GET
//指定服務(wù)數(shù)據(jù)類型,可以是XML,json等數(shù)據(jù)類型
@Produces(MediaType.APPLICATION_XML)
//@Path("/query/{id}")指定該方法的路徑,“{id}”指參數(shù),多個(gè)參數(shù),以“/”隔開,放到“{}”中
@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.創(chuàng)建SEI實(shí)現(xiàn)類
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;
}
}
第五步:配置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">
<!-- <jaxrs:server發(fā)布REST的服務(wù) ,對(duì)JAXRSServerFactoryBean類封裝-->
<jaxrs:server address="/user">
<jaxrs:serviceBeans>
<ref bean="studentInterface"/>
</jaxrs:serviceBeans>
</jaxrs:server>
<!-- 配置服務(wù)實(shí)現(xiàn)類 -->
<bean name="studentInterface" class="com.cad.rest.StudentInterfaceImpl"/>
</beans>
6.配置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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ws_2_cxf_spring_server</display-name>
<!-- 設(shè)置spring的環(huán)境 -->
<context-param>
<!--contextConfigLocation是不能修改的 -->
<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>
7.部署到Tomcat中,發(fā)布服務(wù),測(cè)試一下 http://127.0.0.1:8080/CXFRestDemo/ws/user/Student/query/100
綜合案例:手機(jī)歸屬地查詢
1.創(chuàng)建web項(xiàng)目,導(dǎo)入 CXF jar包
2.生成公網(wǎng)提供的手機(jī)歸屬地查詢的客戶端 wsdl2java -p com.cad.mobile -d . http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

3.編寫我們自己的SEI接口
@WebService
public interface MobileInterface {
public String queryMobile(String phoneNum);
}
4.編寫我們的SEI實(shí)現(xiàn)類 ,里面調(diào)用公網(wǎng)客戶端的查詢方法,我們?cè)赟pring配置客戶端,然后注入即可
public class MobileInterfaceImpl implements MobileInterface {
//公網(wǎng)客戶端,提供set方法 以便注入
private MobileCodeWSSoap mobileClient;
//調(diào)用公網(wǎng)的查詢方法
public String queryMobile(String phoneNum) {
return mobileClient.getMobileCodeInfo(phoneNum, "");
}
public MobileCodeWSSoap getMobileClient() {
return mobileClient;
}
public void setMobileClient(MobileCodeWSSoap mobileClient) {
this.mobileClient = mobileClient;
}
}
5.配置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">
<!-- 配置公網(wǎng)客戶端 -->
<jaxws:client id="mobileClient" address="http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx"
serviceClass="com.cad.mobile.MobileCodeWSSoap"/>
<!-- <jaxws:server發(fā)布我們的服務(wù)-->
<jaxws:server address="/mobile" serviceClass="com.cad.server.MobileInterface">
<jaxws:serviceBean>
<ref bean="mobileServer"/>
</jaxws:serviceBean>
</jaxws:server>
<!-- 配置我們的服務(wù)實(shí)現(xiàn)類 -->
<bean name="mobileServer" class="com.cad.server.MobileInterfaceImpl">
<property name="mobileClient" ref="mobileClient"/>
</bean>
</beans>
6.創(chuàng)建查詢頁(yè)面
<body>
<form action="MobileServlet" method="post">
手機(jī)號(hào)歸屬地查詢:<input type="text" name="phoneNum"><input type="submit" value="查詢"><br>
查詢結(jié)果:${result}
</form>
</body>
7.創(chuàng)建處理的Servlet
@WebServlet("/MobileServlet")
public class MobileServlet extends HttpServlet {
private MobileInterface mobileServer;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取頁(yè)面的電話號(hào)
String phoneNum = request.getParameter("phoneNum");
if(null != phoneNum && !"".equals(phoneNum)){
//獲取Spring容器
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
//獲取我們的服務(wù)實(shí)現(xiàn)類
mobileServer = (MobileInterface) context.getBean("mobileServer");
//調(diào)用查詢方法
String result = mobileServer.queryMobile(phoneNum);
request.setAttribute("result", result);
}
//請(qǐng)求轉(zhuǎn)發(fā)
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
8.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MobileDemo</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>
<context-param>
<!--contextConfigLocation是不能修改的 -->
<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>
</web-app>
9.部署Tomcat,訪問(wèn)測(cè)試

到此這篇關(guān)于Java WebService開源框架CXF詳解的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JavaWeb實(shí)現(xiàn)文件上傳功能詳解
- JavaWeb使用mvc模式實(shí)現(xiàn)登錄功能
- JavaWEB中Servlet的生命周期詳解
- java?web項(xiàng)目Session獲取不到問(wèn)題及解決
- 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)
- 詳解JavaWeb如何實(shí)現(xiàn)文件上傳和下載功能
- Javaweb實(shí)戰(zhàn)之實(shí)現(xiàn)蛋糕訂購(gòu)系統(tǒng)
- 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)
- Java webSerivce的使用看完你就明白了
相關(guān)文章
spring cloud整合ribbon問(wèn)題及解決方案
很多小伙伴在整合ribbon都出了相同的問(wèn)題,今天特地為大家整理了該問(wèn)題的解決方案,文中有非常詳細(xì)的圖文解說(shuō),對(duì)出現(xiàn)同樣問(wèn)題的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
IDEA基于支付寶小程序搭建springboot項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IDEA基于支付寶小程序搭建springboot項(xiàng)目的詳細(xì)步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04
淺談Java回收對(duì)象的標(biāo)記和對(duì)象的二次標(biāo)記過(guò)程
這篇文章主要介紹了淺談Java回收對(duì)象的標(biāo)記和對(duì)象的二次標(biāo)記過(guò)程的相關(guān)內(nèi)容,小編覺(jué)得還是挺不錯(cuò)的,這里給大家分享一下,需要的朋友可以參考。2017-10-10
springboot2如何集成ElasticSearch6.4.3
這篇文章主要介紹了springboot2如何集成ElasticSearch6.4.3問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
java文件的簡(jiǎn)單讀寫操作方法實(shí)例分析
這篇文章主要介紹了java文件的簡(jiǎn)單讀寫操作方法,結(jié)合實(shí)例形式分析了java文件流進(jìn)行讀寫操作的方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
詳解Spring-Cloud2.0之Feign調(diào)用遠(yuǎn)程服務(wù)指南
這篇文章主要介紹了詳解Spring-Cloud2.0之Feign調(diào)用遠(yuǎn)程服務(wù)指南,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
packages思維及使用Java添加Android平臺(tái)特定實(shí)現(xiàn)
這篇文章主要為大家介紹了packages思維及使用Java添加Android平臺(tái)特定實(shí)現(xiàn)在Flutter框架里的體現(xiàn)和運(yùn)用詳解,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12

