使用cxf开发webService(一)

本文详细介绍了使用Java JDK、Eclipse、Apache CXF框架进行Web服务开发的过程,包括创建项目、添加所需jar包、实现接口类及其实现类,以及通过Endpoint发布服务。通过实例演示了如何创建和调用一个简单的Web服务接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备:JDK、Eclipse

所需要jar可在我的资源中下载

所需要jar可在我的资源中下载

jar包下载(官网)目前2.7.7下载Binary  distribution:

http://cxf.apache.org/download.html

下载完成,好了创建项目:

创建项目:

添加jar包:本次使用:

项目目录结构:

代码:

接口类IStudent:

package com.huzhe.service;

import java.util.List;

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

import com.huzhe.po.Student;

@WebService
public interface IStudent {
	
	String sayHi(@WebParam(name= "name")String name);
	
	List<Student> InsertStudent(@WebParam(name="student,num")Student student,int num);

}

接口实现类StudentImpl

package com.huzhe.service;

import java.util.ArrayList;
import java.util.List;

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

import com.huzhe.po.Student;

@WebService(endpointInterface="com.huzhe.service.IStudent",serviceName="StudentImplService")
public class StudentImpl implements IStudent {

	@Override
	public String sayHi(@WebParam(name = "name") String name) {
		 System.out.println("sayHi called");  
	     return "hi  " + name;  
	}

	@Override
	public List<Student> InsertStudent(
			@WebParam(name = "student,num") Student student, int num) {
		System.out.println("向List中添加"+num+"个student");
		List<Student> stus = new ArrayList<Student>();
		for(int i = 0;i<num;i++){
			stus.add(student);
		}
		return stus;
	}
	
	
	
	

}

po:

package com.huzhe.po;

public class Student {
	
	private String id;
	private String name;
	private Integer age;
	
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	

	

}

测试类:

package com.huzhe.test;

import javax.xml.ws.Endpoint;

import com.huzhe.service.StudentImpl;

public class ServieTest {
	 protected ServieTest() throws Exception {  
	        System.out.println("Starting Server");  
	        StudentImpl implementor = new StudentImpl();  
	        String address = "http://localhost:8080/cxf";  
	        Endpoint.publish(address, implementor);  
	    }  
	  
	    public static void main(String args[]) throws Exception {  
	        new ServieTest();  
	        System.out.println("Server ready...");  
	  
	        Thread.sleep(5 * 60 * 1000);  
	        System.out.println("Server exiting");  
	        System.exit(0);  
	    }  
}

启动成功后,访问:http://localhost:8080/cxf?wsdl

如图:








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值