1.建工程添jar包改配置,用myeclipse自带的spring插件完成构建。
说明:先用自带的完成,然后删除jia包重新添加进去我们自己准备的jia包,目的是为了用自动生成的web.xml和applicationContext.xml。
2.修改applicationContext.xml,参考前一页PPT官网。
<?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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<jaxws:endpoint id="helloworld" implementor="com.atguigu.cxf.spring.HelloWorldImpl" address="/HelloWorld"></jaxws:endpoint>
</beans>
3.修改web.xml 关键类:org.apache.cxf.transport.servlet.CXFServlet
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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">
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener</listener-class >
</listener>
<context-param>
<param-name> contextConfigLocation</param-name >
<param-value> classpath:applicationContext.xml</param-value >
</context-param>
<servlet>
<servlet-name> cxf01</ servlet-name>
<servlet-class> org.apache.cxf.transport.servlet.CXFServlet</servlet-class >
</servlet>
<servlet-mapping>
<servlet-name> cxf01</ servlet-name>
<url-pattern> /services/*</url-pattern >
</servlet-mapping>
</web-app>
4.我们的代码:
(1) 接口
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface HelloWorld
{
@WebMethod
@WebResult(name = "sayHelloResult")
public String sayHello(@WebParam(name = "userName") String name,@WebParam(name = "userAge") int age);
}
(2) 实现类
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
public class HelloWorldImpl implements HelloWorld
{
@Override
@WebMethod
@WebResult(name = "sayHelloResult")
public String sayHello(@WebParam(name = "userName") String name,@WebParam(name = "userAge") int age)
{
return "spring say hello to: " + name + "\t" + "age: " + age;
}
}
5.整合:
将以上的项目部署到Tomcat中进行启动,在浏览器端输入:http://localhost:8080/cxf_spring/services 页面出现wsdl文件访问路径,点击进去,如果有wsdl报文出现,就证明部署成功。
说明:这样做service端就可以,打成jia包直接就可以供其他人调用了。
PS:若想结合源码更好的理解WebService,请:https://github.com/luomingkui/WebService