您的位置:首页 > 其它

Web Service -- 使用CXF开发入门

2015-07-18 15:43 405 查看
【使用CXF开发】

一、使用CXF开发Web Service服务器端:

/******************************

每个Web Service组件需要两个部分,接口和实现类

*******************************/

1.开发一个Web Service业务接口(该接口要用@WebService修饰)

2.开发一个Web Service实现类(实现类也需要用@WebService修饰)

3.使用EndPoint类的静态方法来发布Web Service

package com.jjyy.server.service;

import java.util.List;

import javax.jws.WebService;

import com.jjyy.server.domain.Cat;
import com.jjyy.server.domain.User;

@WebService
public interface HelloWorld {

public void sayHello(String name);

List<Cat> getCatsByName(User user);
}


package com.jjyy.server.service.impl;

import java.util.List;

import javax.jws.WebService;

import com.jjyy.server.domain.Cat;
import com.jjyy.server.domain.User;
import com.jjyy.server.service.HelloWorld;
import com.jjyy.server.service.UserService;

@WebService(endpointInterface="com.jjyy.server.service.HelloWorld",name="helloWorld")
public class HelloWorldWS implements HelloWorld {

@Override
public void sayHello(String name) {
System.out.println("当前时间是:"+System.currentTimeMillis()+"==="+name);

}

@Override
public List<Cat> getCatsByName(User user) {
//在实际的项目中,Web Service 组件自己并不会去实现业务功能,
//它只是调用业务逻辑组件的方法来暴露Web Service
UserService us = new UserServiceImpl();
return us.getCatsByUser(user);
}

}


package com.jjyy.server.service;

import java.util.List;

import com.jjyy.server.domain.Cat;
import com.jjyy.server.domain.User;

public interface UserService {

List<Cat> getCatsByUser(User user);

}


package com.jjyy.server.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jjyy.server.domain.Cat;
import com.jjyy.server.domain.User;
import com.jjyy.server.service.UserService;

public class UserServiceImpl implements UserService {

static Map<User, List<Cat>> catDb = new HashMap<User, List<Cat>>();
static{
List<Cat> catList01 = new ArrayList<Cat>();
catList01.add(new Cat(1, "jy01", "yellow"));
catList01.add(new Cat(2, "jy02", "pink"));
List<Cat> catList02 = new ArrayList<Cat>();
catList02.add(new Cat(3, "jy03", "yellow"));
catList02.add(new Cat(4, "jy04", "pink"));
catDb.put(new User(1, "jy1", "123456", "bj"), catList01);
catDb.put(new User(2, "jy2", "123456", "bj"), catList02);
}

@Override
public List<Cat> getCatsByUser(User user) {
return catDb.get(user);
}

}


package com.jjyy.server.main;

import javax.xml.ws.Endpoint;

import com.jjyy.server.service.HelloWorld;
import com.jjyy.server.service.impl.HelloWorldWS;

public class ServerMain {
public static void main(String[] args) {

HelloWorld hw = new HelloWorldWS();
//调用endPont的publish发布webService
Endpoint.publish("http://192.168.0.138/creazy", hw);
System.out.println("web Service start .... ed");
}
}


服务端依赖的jar包:



二、使用CXF开发Web Service客户端:

配置CXF的环境变量

cmd中进入到client的src目录下,执行命令: wsdl2java命令 + 服务端发布的地址



package com.jjyy.client.main;

import java.util.List;

import com.jjyy.server.service.Cat;
import com.jjyy.server.service.HelloWorld;
import com.jjyy.server.service.User;
import com.jjyy.server.service.impl.HelloWorldWSService;

public class ClientMain {

public static void main(String[] args) {
HelloWorldWSService factory = new HelloWorldWSService();
//返回web Service的远程代理
HelloWorld hw = factory.getHelloWorldPort();
hw.sayHello("jiangyhu");
User user = new User();
user.setName("jy1");
user.setPass("123456");
List<Cat> list = hw.getCatsByName(user);
for (Cat cat : list) {
System.out.println(cat.getName()+"=="+cat.getColor());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: