Maven:
<!-- WebService -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.3</version>
</dependency>
这里的org.apache.neethi的版本最好是3.0.3 不然你使用了别人的webservice可能会报错 我试过用3.0.2报错
: javax.wsdl.WSDLException: WSDLException: faultCode=PARSER_ERROR: java.lang.RuntimeException: Cannot create a secure XMLInputFactory
具体原因不清楚
还有记得别加这个
不然还是报错
Caused by: org.apache.cxf.BusException: No DestinationFactory was found for the namespace http://schemas.xmlsoap.org/soap/http.
webxml:
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
url-pattern自定义,就是你访问webservice的地址
Springxml:
<!-- 接口的实现类声明 -->
<bean id="自定义" class="写你webservice接口的实现类" />
<!-- 默认自带日志输出 -->
<bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" />
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
<!-- 发布WebService -->
<jaxws:server id="自定义" serviceClass="这里写你接口不是实现类"
address="/自定义访问webservice的地址">
<jaxws:serviceBean>
<ref bean="自定义接口的实现类" />
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean="inMessageInterceptor" />
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor" />
</jaxws:outInterceptors>
</jaxws:server>
webservice接口:
package ws.spring.server;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface UserWS {
@WebMethod
public UserBean getUserById(int id);
}
webservice实现类 :
package ws.spring.server;
import javax.jws.WebService;
@WebService
public class UserWSImpl implements UserWS {
public UserWSImpl(){
System.out.println("初始化 UserWSImpl");
}
@Override
public UserBean getUserById(int id) {
System.out.println("server getUserById:"+id);
return new UserBean(1, "张三");
}
}
运行输入地址: /ws/自定义的webservice 地址