cxf 方式下的webservice服务端开发

开发环境:myeclipse 8.6+tomcat 6.0,例子,完成一个调用webservice服务端保存一个用户信息的demo。根据网上相关网友的代码和介绍,在myeclipse 里完成了一个简单的webservice服务端。

以下是工程目录:文章后面附源码。源码下载

cxf 方式下的webservice服务端开发_第1张图片

开发步骤:

1、首先在Myeclipse里新建一个web project.在lib中加入以下jar包:

abdera-core-0.4.0-incubating.jar
abdera-extensions-json-0.4.0-incubating.jar
abdera-extensions-main-0.4.0-incubating.jar
abdera-i18n-0.4.0-incubating.jar
abdera-parser-0.4.0-incubating.jar
ant-1.6.5.jar
antlr-2.7.7.jar
aopalliance-1.0.jar
asm-2.2.3.jar
axiom-api-1.2.7.jar
axiom-impl-1.2.7.jar
bcprov-jdk15-1.43.jar
commons-codec-1.3.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
commons-pool-1.5.2.jar
cxf-2.1.9.jar
cxf-manifest.jar
FastInfoset-1.2.7.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.3.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-jms_1.1_spec-1.1.1.jar
geronimo-servlet_2.5_spec-1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.9.jar
jaxb-xjc-2.1.9.jar
jaxen-1.1.jar
jdom-1.0.jar
jettison-1.1.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
jra-1.0-alpha-4.jar
js-1.6R7.jar
jsr311-api-0.8.jar
neethi-2.0.4.jar
oro-2.0.8.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
serializer-2.7.1.jar
slf4j-api-1.5.8.jar
slf4j-jdk14-1.5.8.jar
spring-beans-2.0.8.jar
spring-context-2.0.8.jar
spring-core-2.0.8.jar
spring-dao-2.0.8.jar
spring-jms-2.0.8.jar
spring-support-2.0.8.jar
spring-web-2.0.8.jar
velocity-1.6.2.jar
WHICH_JARS
wsdl4j-1.6.2.jar
wss4j-1.5.8.jar
wstx-asl-3.2.9.jar
xalan-2.7.1.jar
xml-resolver-1.2.jar
xmlbeans-2.4.0.jar
XmlSchema-1.4.5.jar
xmlsec-1.4.3.jar
文后会以附件的形式打包。

2、然后便可以开始配置:

在WEB-INF 下新建webservice.xml;名称可自定义。

webservice.xml的内容如下

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	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">

	<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="newUserInfo" class="demo.spring.impl.NewUserInfoServiceImpl" />

	<jaxws:endpoint id="newUserInfoService" implementor="#newUserInfo" address="/newUserInfoService"></jaxws:endpoint>

</beans>


在web.xml中为以下内容:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/webservice.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>CXFServlet</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFServlet</servlet-name>
		<url-pattern>/service/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
3、代码编写:


定义实体bean类:UserInfoInput.java和UserInfoResponse.java


package demo.spring.bean;

/**
 * 用户信息实体类
 *
 * @author thebigboy
 *
 */
public class UserInfoInput {
	
	private String userid ;
	
	private String username ;
	
	private int age;
	
	private boolean sex ;
	
	private String home;

	public UserInfoInput() {
		super();
	}

	public UserInfoInput(String userid, String username, int age, boolean sex,
			String home) {
		super();
		this.userid = userid;
		this.username = username;
		this.age = age;
		this.sex = sex;
		this.home = home;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public boolean isSex() {
		return sex;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public String getHome() {
		return home;
	}

	public void setHome(String home) {
		this.home = home;
	}
	
	@Override
	public String toString() {
		StringBuffer sbr = new StringBuffer();
		sbr.append("[userid=");
		sbr.append(userid);
		sbr.append(",");
		sbr.append("username=");
		sbr.append(username);
		sbr.append(",");
		sbr.append("age=");
		sbr.append(age);
		sbr.append(",");
		sbr.append("sex=");
		sbr.append(sex==true?"1":0);
		sbr.append(",");
		sbr.append("home");
		sbr.append(home);
		sbr.append("]"); 
		return sbr.toString();
	}
}
package demo.spring.bean;

public class UserInfoResponse {
	private String userid ;
	
	private String username ;
	
	private int age;
	
	private boolean sex ;
	
	private String home;

	public UserInfoResponse() {
		super();
	}

	public UserInfoResponse(String userid, String username, int age, boolean sex,
			String home) {
		super();
		this.userid = userid;
		this.username = username;
		this.age = age;
		this.sex = sex;
		this.home = home;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public boolean isSex() {
		return sex;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public String getHome() {
		return home;
	}

	public void setHome(String home) {
		this.home = home;
	}

	
	@Override
	public String toString() {
		StringBuffer sbr = new StringBuffer();
		sbr.append("[userid=");
		sbr.append(userid);
		sbr.append(",");
		sbr.append("username=");
		sbr.append(username);
		sbr.append(",");
		sbr.append("age=");
		sbr.append(age);
		sbr.append(",");
		sbr.append("sex=");
		sbr.append(sex==true?"1":0);
		sbr.append(",");
		sbr.append("home");
		sbr.append(home);
		sbr.append("]"); 
		return sbr.toString();
	}
	
}


先定义接口:NewUserInfoService.java


package demo.spring;

import javax.jws.WebParam;
import javax.jws.WebService;

import demo.spring.bean.UserInfoInput;
import demo.spring.bean.UserInfoResponse;

@WebService
public interface NewUserInfoService {

	UserInfoResponse addUserRequest(
			@WebParam(name = "userInfo", targetNamespace = "http://spring.demo/") UserInfoInput userInfo);
}
接口的实现:NewUserInfoServiceImpl.java,这里可以写客户端调用webservice,服务端里的具体的逻辑处理。


package demo.spring.impl;

import javax.jws.WebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import demo.spring.NewUserInfoService;
import demo.spring.bean.UserInfoInput;
import demo.spring.bean.UserInfoResponse;

@WebService(endpointInterface = "demo.spring.NewUserInfoService")
public class NewUserInfoServiceImpl implements NewUserInfoService{

	private Logger logger = LoggerFactory.getLogger(NewUserInfoServiceImpl.class);
	
	@Override
	public UserInfoResponse addUserRequest(UserInfoInput userInfoInput) {
		logger.info("Client调用 webservice输入的用户信息是:"+userInfoInput.toString());
		UserInfoResponse userInfoResponse = new UserInfoResponse();
		userInfoResponse.setUserid("1  "+userInfoInput.getUserid());
		userInfoResponse.setAge( 1+ userInfoInput.getAge());
		userInfoResponse.setHome("1  "+userInfoInput.getHome());
		userInfoResponse.setSex(false);
		userInfoResponse.setUsername("1  "+userInfoInput.getUsername());
		return userInfoResponse;
	}

}
最后一步,在发布到tomcat上的时候,

注:如使用jdk1.6进行开发,需下载jaxb-api.jarjaxws-api.jar,然后在本机安装JDK的地方,在jdk1.6.0jre文件夹下的lib文件夹中新建endorsed文件夹,放入以上两个jar包才可以进行开发说明见:http://www.blogjava.net/ashutc/archive/2009/11/24/303521.html

发布到tomcat之后,访问

http://{IP}:{port}/{项目名}/service/,该链接会拦截,列出该项目下所有的服务:

如图:点击相应的链接即可进入某一个接口。cxf 方式下的webservice服务端开发_第2张图片

例如我的本地地址是: http://10.23.24.247:8080/wsdlTest/service/

源码见:http://download.csdn.net/detail/mengjunxiaoxiang/6317571,导入myeclipse即可使用。

后面再讲解如何通过服务端生成wsdl文件,供客户端生成 java文件,调用等等。


你可能感兴趣的:(webservice,MyEclipse,CXF,服务端)