CXF编写webservice Demo及遇见问题

1. Demo (转载 http://mushme.iteye.com/blog/1776078)

cxf的官网文档地址:http://cxf.apache.org/docs/ 
1.建立一个web project,名称为cfx-new,部署目录写成ROOT 
2.下载最新的cxf包http://cxf.apache.org/download.html,我下载的是apache-cxf-2.5.2.zip,直接将lib下的所有jar拷贝到lib下 

3.新建一个接口类HelloWorld 

package info.frady.service;  
  
import javax.jws.WebParam;  
import javax.jws.WebService;  
  
@WebService  
public interface HelloWorld {  
    String sayHi(String text);  
}  

4.建立一个接口的实现类HelloWorldImpl 

package info.frady.service.impl;  
  
import info.frady.service.HelloWorld;  
  
import javax.jws.WebService;  
  
@WebService(endpointInterface = "info.frady.service.HelloWorld",   
            serviceName = "helloWorld")  
public class HelloWorldImpl implements HelloWorld {  
   
    public String sayHi(String text) {  
        return "Hello " + text;  
    }  
}
说明下: 
*@WebService:申明为webservice的注解,这样cxf就知道这是个webservice了 
*endpointInterface:要暴露的接口类 
 *serviceName :    服务名,其实就是调用地址 

5.在WEB-INF目录下新建beans.xml,如下: 

<?xml version="1.0" encoding="UTF-8"?>  
<!-- START SNIPPET: beans -->  
<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.xsd  
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  
    <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    <jaxws:endpoint   
      id="helloWorld"   
      implementor="info.frady.service.impl.HelloWorldImpl"   
      address="/helloWorld" />  
</beans>  
说明: 
注: implementor :接口类的实现类 
        address:   要和注释里面声明的服务名对应,对应的是外部访问地址 

6.在修改web.xml文件,如下

<?xml version="1.0" encoding="ISO-8859-1"?>  
  
<!DOCTYPE web-app  
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  
    "http://java.sun.com/dtd/web-app_2_3.dtd">  
  
<!-- START SNIPPET: webxml -->  
<web-app>  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/beans.xml</param-value>  
    </context-param>  
  
    <listener>  
        <listener-class>  
            org.springframework.web.context.ContextLoaderListener  
        </listener-class>  
    </listener>  
  
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <display-name>CXF Servlet</display-name>  
        <servlet-class>  
            org.apache.cxf.transport.servlet.CXFServlet  
        </servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>  
</web-app>  
<!-- END SNIPPET: webxml -->  
7.启动tomcat 
8.输入地址http://localhost:9081/services/,出现WSDL : {http://impl.service.frady.info/}HelloWorld说明服务端成功。 

9.编写测试代码 

import info.frady.domain.Person;  
import info.frady.service.HelloWorld;  
  
import org.apache.cxf.interceptor.LoggingInInterceptor;  
import org.apache.cxf.interceptor.LoggingOutInterceptor;  
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  
  
public class TestHello {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
        factory.getInInterceptors().add(new LoggingInInterceptor());  
        factory.getOutInterceptors().add(new LoggingOutInterceptor());  
        factory.setServiceClass(HelloWorld.class);  
        factory.setAddress("http://localhost:8080/services/HelloWorld");  
        HelloWorld client = (HelloWorld) factory.create();  
        String reply = client.sayHi("frady");  
        System.out.println("Server said: " + reply);      
        System.exit(0);   
    }  
  
} 

至此Demo结束

webservice接口问题

1.CXF实现webservice出错
Caused by: javax.xml.ws.WebServiceException: javax.xml.ws.WebServiceException: Attributes portName, serviceName and endpointInterface are not allowed in the @WebService annotation of an SEI.
***这里写的是接口名 ,写错了写成实现类会报@WebService注解失败没有endpointInterface 和 serviceName

2.关于webservice框架cxf遇到的一些问题
警告: Interceptor for {http://impl.service.test.com/}TestService has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault: No binding operation info while invoking unknown method with params unknown.
***参照https://blog.csdn.net/wangsenhong/article/details/18036297
解决方式: 

将访问地址修改为http://localhost:8080/cxf/TestService?wsdl即可

Jaxb处理java对象和xml之间转换
注意,如果返回值是对象,对象中的字段为空时返回的xml中是没有该字段,需要做显示处理
 if(ob == null || ob.equals("")){  
            ob=" ";  
        }  
将字段的值设置成“ ”,这样返回的xml中就会有该字段

参照https://blog.csdn.net/yusewuhen/article/details/44150833

http://www.cnblogs.com/fragranting/archive/2012/03/25/xml--jaxb.html

3. 如果接口返回的是对象,返回的对象得实现serialze接口

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页