maven整合webservice(cxf)的时候可能会出现的问题

来源:互联网 发布:《c专家编程》 编辑:程序博客网 时间:2024/05/16 01:45

1 使用的maven包,将cxf的jar包信息放到pom.xml中会出现错误。(cxf-rt-frontend-jaxws),将此包放入到pomxml中的时候会报错,再加入cxf-rt-transports-http包时错误会消失

贴出以下信息。

<!--webservice所需要的包-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.6</version>
</dependency>

2.配置web.xml的时候注意将以下信息放在DispatcherServlet之前。

<!--webservice-->
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/webservice/*</url-pattern>  
    </servlet-mapping>

<context-param>

3.在配置webxml时不要忘记将配置文件引入到spring的扫描路径中

4在使用@webservice注解时,可能会出现Access restriction: The type 'WebService' is not API (restriction on require....等警告,并且IDE中不会提示此标签,修复了一下就好了,原因不明,有知道的可以告诉我,共同学习

5在运行的时候可能会报错:Configuration problem: Failed to import bean definitions from URL location [classpath:META-INF/cxf/cxf-extension-soap.xml]

原因是使用了2.4以下的配置却使用的2.4以上的jar包,官方有说明如下:

Changes in CXF 2.4.x

The below is applicable for CXF versions 2.3.x and older. Starting in CXF 2.4.0, the extensions are loaded internally by CXF automatically and you do not need to import all the cxf-extension-*.xml file. You only need to import classpath:META-INF/cxf/cxf.xml.

在使用2.3或者更早的版本的时候你需要引入cxf-extension-*.xml 从2.4开始你就不用引入这个xml了只需要引入<import resource="classpath:META-INF/cxf/cxf.xml" />  


5在编写cxf的同时,要注意一点为不要将@webservice的注解添加在接口上,请直接添加到实现类上,不然可能会出现的问题是,在用jaxws调用时出现XXXX是接口,而jaxws无法处理接口。

最后贴一下cxf的配置文件和helloworld


spring-cxf.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:http-conf = "http://cxf.apache.org/transports/http/configuration"    
    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  
        http://cxf.apache.org/transports/http/configuration      
        http://cxf.apache.org/schemas/configuration/http-conf.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="hello"
implementor="com.nuotai.webservice.impl.helloWorldImpl" address="/hello" />
</beans> 



helloWorld.java


package com.nuotai.webservice;


import javax.jws.WebMethod;
import javax.jws.WebService;


import com.nuotai.model.User;




@WebService
public interface  helloWorld {

@WebMethod
public String sayHello(String name); 
 


@WebMethod
public String sayHello2(User user); 


}


helloWorldImpl.java


package com.nuotai.webservice.impl;


import javax.jws.WebService;


import com.alibaba.fastjson.JSONObject;
import com.nuotai.model.User;
import com.nuotai.webservice.helloWorld;


@WebService
public class helloWorldImpl implements helloWorld {


public String sayHello(String name) {
JSONObject myJsonObject = JSONObject.parseObject(name);
String nameTemp = myJsonObject.getString("name");
/*
* String nameTemp = name.getString("name");
* System.out.println(nameTemp);
*/
return nameTemp;
}


public String sayHello2(User user) {
JSONObject json = new JSONObject();
String name = user.getUser_name();
json.put("name", name);
return json.toJSONString();
}
}

0 0