使用cxf 发布 jax-rs 风格webservice 。并客户端测试。

详细介绍:http://www.ibm.com/developerworks/cn/java/j-lo-jaxrs/


1、定义一个User对象

package com.zf.test;


import java.util.Date;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="User")   
public class User {   
	private long id;   
	private String name;   
	private Date birthday;   
	public long getId() {   
		return id;   
	}   
	public void setId(long id) {   
		this.id = id;   
	}   
	public String getName() {   
		return name;   
	}   
	public void setName(String name) {   
		this.name = name;   
	}   
	public Date getBirthday() {   
		return birthday;   
	}   
	public void setBirthday(Date birthday) {   
		this.birthday = birthday;   
	}   


}  

2、编写Service接口

package com.zf.test;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path(value="/user")   
@Produces(MediaType.TEXT_XML)  //指定返回给客户端的类型
@Consumes(MediaType.TEXT_XML)  //接收客户端来的类型	
public interface IUserService {   
	
	@GET  
	@Path(value="/info/{id}")
	public User getUser(@PathParam("id") long id,@QueryParam("name") String name);   

	@GET  
	@Path(value="/info2")   
	public User getUser(@QueryParam("name") String name);   
	
	@POST
	@Path(value = "/info3")
	public User getUser(User user); 
	
}  

3、实现Service类

package com.zf.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class UserServiceImpl implements IUserService {   

	public User getUser(long id,String name) {   
		User user=new User();   
		user.setId(id);   
		user.setName(name);   
		try {   
			user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1987-02-01"));   
		} catch (ParseException e) {   
			e.printStackTrace();   
		}   
		return user;   
	}   

	public User getUser(String name) {   
		User user=new User();   
		user.setId(1);   
		user.setName(name);   
		try {   
			user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1985-03-01"));   
		} catch (ParseException e) {   
			e.printStackTrace();   
		}   
		return user;   
	}

	@Override
	public User getUser(User user) {
		System.out.println("getUser");
		if(user != null)
			System.out.println( user.getId() + " " + user.getName() + "  " + user.getBirthday().toLocaleString());
		else
			System.out.println("user is null!");
		return user;
	}   

}  

4、编写客户端

可以直接在浏览器中输入 http://localhost:9999/user/info/3?name=is_zhoufeng 访问。 在浏览器中会输出如下结果:

<User><birthday>1987-02-01T00:00:00+08:00</birthday><id>3</id><name>is_zhoufeng</name></User>


(下面的操作之前,应该在客户端生成对应的User类型。可以在服务端提供相应的xsd文件。给客户端生成。我这里直接将User复制过去了)

首先根据服务端 编写客户端Service接口

package com.zf.vo;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("/user")
@Produces(MediaType.TEXT_XML)   
@Consumes(MediaType.TEXT_XML)
public interface UserService {
	
	@POST
	@Path("/info3")
	User info(User user);
	
	@GET  
	@Path(value="/info2")  
	User info2(String name) ;

}



访问GET方法

package com.zf.vo;
import org.apache.cxf.jaxrs.client.WebClient;

public class RestClient {
	public static void main(String[] args) {   
		WebClient client=WebClient.create("http://127.0.0.1:5555/user/info/3?name=is_zhoufeng");
		User user=(User)client.accept("text/xml").get(User.class);   
		System.out.println(user.getName());   
	}   
}

访问POST方法

package com.zf.vo;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PostRestClient {
	public static void main(String[] args) {
		ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","jaxrs-https.xml"});
		UserService countriesService = (UserService)cxt.getBean("userService");
		User user = new User();
		user.setBirthday(new Date());
		user.setId(3);
		user.setName("is_zhoufeng");  
		User u = countriesService.info(user);
		System.out.println(u.getId());  
		System.out.println(u.getName());
		System.out.println(u.getBirthday().toLocaleString());
	}
}


你可能感兴趣的:(使用cxf 发布 jax-rs 风格webservice 。并客户端测试。)