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

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

1、定义一个User对象

[java]  view plain  copy
 
  1. package com.zf.test;  
  2.   
  3.   
  4. import java.util.Date;  
  5.   
  6. import javax.xml.bind.annotation.XmlRootElement;  
  7.   
  8. @XmlRootElement(name="User")     
  9. public class User {     
  10.     private long id;     
  11.     private String name;     
  12.     private Date birthday;     
  13.     public long getId() {     
  14.         return id;     
  15.     }     
  16.     public void setId(long id) {     
  17.         this.id = id;     
  18.     }     
  19.     public String getName() {     
  20.         return name;     
  21.     }     
  22.     public void setName(String name) {     
  23.         this.name = name;     
  24.     }     
  25.     public Date getBirthday() {     
  26.         return birthday;     
  27.     }     
  28.     public void setBirthday(Date birthday) {     
  29.         this.birthday = birthday;     
  30.     }     
  31.   
  32.   
  33. }    

2、编写Service接口

[java]  view plain  copy
 
  1. package com.zf.test;  
  2. import javax.ws.rs.Consumes;  
  3. import javax.ws.rs.GET;  
  4. import javax.ws.rs.POST;  
  5. import javax.ws.rs.Path;  
  6. import javax.ws.rs.PathParam;  
  7. import javax.ws.rs.Produces;  
  8. import javax.ws.rs.QueryParam;  
  9. import javax.ws.rs.core.MediaType;  
  10.   
  11. @Path(value="/user")     
  12. @Produces(MediaType.TEXT_XML)  //指定返回给客户端的类型  
  13. @Consumes(MediaType.TEXT_XML)  //接收客户端来的类型    
  14. public interface IUserService {     
  15.       
  16.     @GET    
  17.     @Path(value="/info/{id}")  
  18.     public User getUser(@PathParam("id") long id,@QueryParam("name") String name);     
  19.   
  20.     @GET    
  21.     @Path(value="/info2")     
  22.     public User getUser(@QueryParam("name") String name);     
  23.       
  24.     @POST  
  25.     @Path(value = "/info3")  
  26.     public User getUser(User user);   
  27.       
  28. }    


3、实现Service类

[java]  view plain  copy
 
  1. package com.zf.test;  
  2.   
  3. import java.text.ParseException;  
  4. import java.text.SimpleDateFormat;  
  5.   
  6. public class UserServiceImpl implements IUserService {     
  7.   
  8.     public User getUser(long id,String name) {     
  9.         User user=new User();     
  10.         user.setId(id);     
  11.         user.setName(name);     
  12.         try {     
  13.             user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1987-02-01"));     
  14.         } catch (ParseException e) {     
  15.             e.printStackTrace();     
  16.         }     
  17.         return user;     
  18.     }     
  19.   
  20.     public User getUser(String name) {     
  21.         User user=new User();     
  22.         user.setId(1);     
  23.         user.setName(name);     
  24.         try {     
  25.             user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("1985-03-01"));     
  26.         } catch (ParseException e) {     
  27.             e.printStackTrace();     
  28.         }     
  29.         return user;     
  30.     }  
  31.   
  32.     @Override  
  33.     public User getUser(User user) {  
  34.         System.out.println("getUser");  
  35.         if(user != null)  
  36.             System.out.println( user.getId() + " " + user.getName() + "  " + user.getBirthday().toLocaleString());  
  37.         else  
  38.             System.out.println("user is null!");  
  39.         return user;  
  40.     }     
  41.   
  42. }    


4、编写客户端

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

[html]  view plain  copy
 
  1. <User><birthday>1987-02-01T00:00:00+08:00</birthday><id>3</id><name>is_zhoufeng</name></User>  


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

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

[java]  view plain  copy
 
  1. package com.zf.vo;  
  2.   
  3. import javax.ws.rs.Consumes;  
  4. import javax.ws.rs.GET;  
  5. import javax.ws.rs.POST;  
  6. import javax.ws.rs.Path;  
  7. import javax.ws.rs.Produces;  
  8. import javax.ws.rs.core.MediaType;  
  9.   
  10.   
  11. @Path("/user")  
  12. @Produces(MediaType.TEXT_XML)     
  13. @Consumes(MediaType.TEXT_XML)  
  14. public interface UserService {  
  15.       
  16.     @POST  
  17.     @Path("/info3")  
  18.     User info(User user);  
  19.       
  20.     @GET    
  21.     @Path(value="/info2")    
  22.     User info2(String name) ;  
  23.   
  24. }  




访问GET方法

[java]  view plain  copy
 
  1. package com.zf.vo;  
  2. import org.apache.cxf.jaxrs.client.WebClient;  
  3.   
  4. public class RestClient {  
  5.     public static void main(String[] args) {     
  6.         WebClient client=WebClient.create("http://127.0.0.1:5555/user/info/3?name=is_zhoufeng");  
  7.         User user=(User)client.accept("text/xml").get(User.class);     
  8.         System.out.println(user.getName());     
  9.     }     
  10. }  

访问POST方法

[java]  view plain  copy
 
    1. package com.zf.vo;  
    2. import java.util.Date;  
    3. import org.springframework.context.ApplicationContext;  
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    5. public class PostRestClient {  
    6.     public static void main(String[] args) {  
    7.         ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","jaxrs-https.xml"});  
    8.         UserService countriesService = (UserService)cxt.getBean("userService");  
    9.         User user = new User();  
    10.         user.setBirthday(new Date());  
    11.         user.setId(3);  
    12.         user.setName("is_zhoufeng");    
    13.         User u = countriesService.info(user);  
    14.         System.out.println(u.getId());    
    15.         System.out.println(u.getName());  
    16.         System.out.println(u.getBirthday().toLocaleString());  
    17.     }  
    18. }  

猜你喜欢

转载自www.cnblogs.com/huangwentian/p/9147764.html