1.去官方下载对应的jar包:http://cxf.apache.org/
2.maven配置相应jar包
3.修改web.xml,完成spring和cxf配置
1 <!-- Spring --> 2 <context-param> 3 <param-name>contextConfigLocation</param-name> 4 <param-value> 5 classpath*:/applicationContext.xml 6 </param-value> 7 </context-param> 8 9 <listener> 10 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 11 </listener> 12 13 <!-- CXFServlet --> 14 <servlet> 15 <servlet-name>CXFServlet</servlet-name> 16 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 17 </servlet> 18 <servlet-mapping> 19 <servlet-name>CXFServlet</servlet-name> 20 <url-pattern>/WebService/*</url-pattern> 21 </servlet-mapping>
4.编写服务接口和实现
1 @WebService(targetNamespace=Constant.NAME_SPACE) 2 public interface FinanceService { 3 4 String financeSearchById(@WebParam(name="id")Integer id); 5 6 }
1 @Transactional 2 public class FinanceServiceImpl implements FinanceService{ 3 4 private static Logger logger = Logger.getLogger(FinanceServiceImpl.class); 5 6 @Resource(name="financeDao") 7 private FinanceDao financeDao; 8 9 @Override 10 public String financeSearchById(Integer id) { 11 String json; 12 13 try{ 14 json=financeDao.financeSearchById(id); 15 }catch(Exception e){ 16 Map<String,Object> map = new HashMap<String, Object>(); 17 map.put("is_success", false); 18 map.put("error_msg", "程序执行出错:"+e.getMessage()); 19 json = JsonUtil.objectToJson(map); 20 } 21 22 return json; 23 } 24 25 }
5.修改spring配置文件。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" 5 xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 6 http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> 8 9 <description>Apache CXF配置</description> 10 11 <import resource="classpath:META-INF/cxf/cxf.xml" /> 12 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 13 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 14 15 <!-- CXF Web Service Server端配置 --> 16 <bean id="wsFinanceServiceImpl" class="com.****.******.business.webservice.server.impl.FinanceServiceImpl"/> 17 <jaxws:server id="wsFinanceService" serviceClass="com.****.******.business.webservice.server.FinanceService" serviceBean="#wsFinanceServiceImpl" address="/financeService" /> 18 19 </beans>
6.tomcat发布运行,http://localhost:8080/******/WebService/financeService?wsdl,看到描述文件即可
7.编写测试类,测试。
1 public class Test { 2 public static void main(String[] args) { 3 JaxWsProxyFactoryBean webService = new JaxWsProxyFactoryBean(); 4 webService.setServiceClass(FinanceService.class); 5 webService.setAddress("http://localhost:8080/******/WebService/financeService?wsdl"); 6 FinanceService financeService = (FinanceService) webService.create(); 7 System.out.println(financeService.financeSearchById(5)); 8 } 9 }
如果能顺利看到结果输出就算成功了。