1.引入jar包 我是从csdn下载下来的,然后就自己保存下来了,http://download.csdn.net/detail/kai763253075/9605349
2.编写接口类,其中总共发布了两个接口方法 sayHello 和sayNihao
package com.webservice;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface ITestWebService {
//@WebParam(name="name")为了增强可读性可以不写
public String sayHello(@WebParam(name = "name") String name);
public User sayNihao(User user);
}
3.编写实现类
package com.webservice;
import javax.jws.WebService;
@WebService(endpointInterface="com.webservice.ITestWebService")
public class TestWebServiceImpl implements ITestWebService {
public String sayHello(String name) {
System.out.println("你好,"+ name);
return "How are you " + name;
}
public User sayNihao(User user) {
System.out.println("nihao:"+user.getName());
user.setAge(80);
return user;
}
}
4.由于我自己写的sayNihao()使用了User类这里也顺便写一下,不是必要步骤不过一般实际开发中参数是实体类的情况太正常了。
package com.webservice;
public class User {
private String name;
private int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
5.cxf配置文件applicationContext-cxf.xml为了看得更文件内容更清楚单独生成一个xml文件,
注意添加
xmlns:jaxws="http://cxf.apache.org/jaxws"
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"等内容,同时注意空格等格式 。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="testWebService" class="com.webservice.TestWebServiceImpl"/>
<!--测试Service -->
<jaxws:server serviceClass="com.webservice.ITestWebService" address="/test">
<jaxws:serviceBean>
<ref bean="testWebService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
6spring配置文件applicationContext.xml 把 applicationContext-cxf.xml加入进去,也可以一起配置到web.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--配置CXF扫描,并制定包目录 -->
<context:component-scan base-package="com.webservice"></context:component-scan>
<import resource="applicationContext-cxf.xml"/>
</beans>
7.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--spring监听 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--CXF配置 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
8.最后部署tomcat发布,访问http://localhost:8080/b2bweb/查看发布的wenservice