javaweb学习路之四--cxf发布Webservice

Posted 执念saying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaweb学习路之四--cxf发布Webservice相关的知识,希望对你有一定的参考价值。

背景:maven构建的springMvc+mybatis框架

源码---》https://github.com/Zering/MyWeb

步骤:(本步骤是自己在实际探索过程中的步骤,我的思路是先简单粗暴的写出方法,报错了再根据错误来解决问题)

  第一步:直接写出了接口和实现类

  示例接口代码

package com.app.service;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService  
@SOAPBinding(style = Style.RPC) 
public interface IWebService {

	public String sayHello(String string);
}

  示例实现类方法

package com.app.service.impl;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.stereotype.Service;

import com.app.service.IWebService;

@Service
@WebService(endpointInterface="com.app.service.IWebService",serviceName="webservice1")
@SOAPBinding(style = Style.RPC)
public class WebserviceImpl implements IWebService {

	@Override
	public String sayHello(String string) {
		return "hello "+string;
	}

}

  第二步:写cxf的配置信息,创建一个application-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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.0.xsd
	       http://cxf.apache.org/jaxws  http://cxf.apache.org/schemas/jaxws.xsd"
	default-autowire="byName">
	 
    <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" />
	
    <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>    
	<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>  
	
	<jaxws:endpoint implementor="com.app.service.impl.WebserviceImpl" address="/webservice1"></jaxws:endpoint>
   
</beans>

  注意:这个文件里面的三个<import>是从jar包中导入的,所以在classpath后面一定要加*号

  步骤三:修改web.xml

    1.修改<context-param>

 

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring-mybatis.xml classpath:application-cxf.xml</param-value>
</context-param>

 

    2.追加一个webservice的servelet

    <!-- For WebService -->
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/WebService/*</url-pattern>
	</servlet-mapping>

  步骤四:这个时候启动tomcat的话,会有异常说缺啥缺啥的,根据这个信息来导入相应的jar包,加的jar包如下:

    <dependency>
	  <groupId>org.apache.cxf</groupId>
	  <artifactId>apache-cxf</artifactId>
	  <version>3.1.6</version>
	  <type>pom</type>
    </dependency>
    <dependency>
       <groupId>org.apache.cxf</groupId>   <artifactId>cxf-rt-bindings-soap</artifactId>   <version>3.1.6</version>     </dependency>

  步骤五:验证,启动tomcat,url输入 http://localhost:8080/MyWeb/WebService

    

    完成。

  

 

  

以上是关于javaweb学习路之四--cxf发布Webservice的主要内容,如果未能解决你的问题,请参考以下文章

基于cxf框架javaweb服务说明

springboot 整合 cxf 发布webservice与调用

编程路之始

React漫漫学习路之 React Router

怎么用cxf的wsdl2java解析wcf生成的wsdl

React漫漫学习路之 利用Create React App命令创建一个React应用