CXF + Spring + Maven 开发WebService

一、背景介绍

  由于工作需要,研究了下CXF框架,并使用CXF框架做了个小demo,一方面是为了记录下来供自己复习用,另一方面方便大家学习使用。

二、什么是cxf

  Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构,支持JAX-WS。

三、基于CXF的webservice开发

  (1)创建maven项目

  pom文件代码如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.chinaunicom.cxf</groupId>
	<artifactId>CXF_Demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

        <!-- 这里直接导入整个项目下的所有jar包,其中去除几个 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>apache-cxf</artifactId>
			<version>3.2.6</version>
			<type>pom</type>
			<exclusions>
				<exclusion>
					<artifactId>cxf-services-wsn-api</artifactId>
					<groupId>org.apache.cxf.services.wsn</groupId>
				</exclusion>
				<exclusion>
					<artifactId>cxf-services-wsn-co
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是一个简单的SSM+CXF+WebService+JSP的demo: 1. 创建一个Maven项目,添加以下依赖: ```xml <dependencies> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.18.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.18.RELEASE</version> </dependency> <!-- CXF --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.4.0</version> </dependency> <!-- JSP --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> ``` 2. 配置web.xml文件: ```xml <web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/dispatcherServlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 3. 配置applicationContext.xml文件: ```xml <beans> <!-- 配置数据源 --> <!-- 配置MyBatis --> <!-- 配置Spring事务管理 --> <!-- 配置WebService服务 --> <bean id="helloService" class="com.example.service.HelloServiceImpl"/> <bean id="cxf" class="org.apache.cxf.jaxws.JaxWsServerFactoryBean"> <property name="serviceClass" value="com.example.service.HelloService"/> <property name="address" value="/hello"/> <property name="serviceBean" ref="helloService"/> </bean> </beans> ``` 4. 配置dispatcherServlet.xml文件: ```xml <beans> <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <context:component-scan base-package="com.example.controller"/> </beans> ``` 5. 创建HelloService接口: ```java @WebService public interface HelloService { @WebMethod String sayHello(String name); } ``` 6. 创建HelloServiceImpl实现类: ```java @Service public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 7. 创建一个Controller: ```java @Controller public class HelloController { @Autowired private HelloService helloService; @RequestMapping("/hello") public ModelAndView hello(String name) { String message = helloService.sayHello(name); ModelAndView mav = new ModelAndView("hello"); mav.addObject("message", message); return mav; } } ``` 8. 创建一个JSP视图,放在/WEB-INF/views/hello.jsp: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 9. 启动Tomcat服务器,访问http://localhost:8080/hello?name=world,应该能够看到页面上显示"Hello, world!"。 以上就是一个简单的SSM+CXF+WebService+JSP的demo,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值