maven+spring+cxf编写web service

参见文章
1.创建项目

mvn archetype:generate -DarchetypeCatalog=Internal
选择19,创建web项目

2.生成eclipse项目,参见文章
3.修改web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Archetype Created Web Application</display-name>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>
</web-app>
4.创建webservice接口
package com.sysware.demo.app.service.inf;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.sysware.demo.app.model.RetInfo;

@WebService
public interface IGetInfoService
{
	@WebMethod(operationName = "add")
	@WebResult(name = "result")
	public int add(@WebParam(name = "num1") int num1,
			@WebParam(name = "num2") int num2);
	
	@WebMethod(operationName = "getRetInfo")
    @WebResult(name = "result")
    public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age);
}
5.创建webservice实现类
package com.sysware.demo.app.service.impl;

import javax.jws.WebService;

import com.sysware.demo.app.model.RetInfo;
import com.sysware.demo.app.service.inf.IGetInfoService;

@WebService(endpointInterface = "com.sysware.demo.app.service.inf.IGetInfoService")
public class GetInfoServiceImpl implements IGetInfoService
{

	@Override
	public int add(int num1, int num2)
	{
		return num1 + num2;
	}

	@Override
	public RetInfo getRetInfo(String name, int age)
	{
        RetInfo retInfo = new RetInfo();
        retInfo.setAge(age);
        retInfo.setName(name);
        return retInfo;
	}

}
6.返回对象
package com.sysware.demo.app.model;

public class RetInfo
{
    private String name;
    private int age;
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
}
7.创建bean文件applicationContext.xml在WEB-INF目录下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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">
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>	
	<bean id="getInfoServiceImpl" class="com.sysware.demo.app.service.impl.GetInfoServiceImpl"></bean>
	<jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint>
</beans>
8.修改pom文件
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sysware.demo</groupId>
	<artifactId>mvncxfdemo</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>mvncxfdemo Maven Webapp</name>
	<url>http://maven.apache.org</url>	
        <dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<type>jar</type>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>2.6.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>3.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-common</artifactId>
			<version>2.5.4</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-core</artifactId>
			<version>2.6.1</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>2.6.1</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>mvncxfdemo</finalName>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>8.1.5.v20120716</version>
				<configuration>
					<stopPort>9966</stopPort>
					<stopKey>foo</stopKey>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
9.运行
mvn clean jetty:run-war

10.或者实现接口修改为

package com.sysware.demo.app.service.impl;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.sysware.demo.app.model.RetInfo;

@WebService
public class GetInfoServiceImpl
{

	@WebMethod(operationName = "add")
	@WebResult(name = "result")
	public int add(@WebParam(name = "num1") int num1,
			@WebParam(name = "num2") int num2)
	{
		return num1 + num2;
	}

	@WebMethod(operationName = "getRetInfo")
    @WebResult(name = "result")
    public RetInfo getRetInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age)
	{
		RetInfo retInfo = new RetInfo();
		retInfo.setAge(age);
		retInfo.setName(name);
		return retInfo;
	}

}

由于客户端用gsoap,如果两个service在同一个目录下,将导致错误,因为两个targetNamespace相同

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://userlist.user.webservice.service.cms.ivideo.sysware.com/" elementFormDefault="unqualified" targetNamespace="http://userlist.user.webservice.service.cms.ivideo.sysware.com/" version="1.0">

为了保证两个service不在一个target里面,建议每个package下只有一个webservice

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值