Use CXF to publish Jax-rs style webservice. and client testing.

Source: Internet
Author: User

Detailed Introduction: http://www.ibm.com/developerworks/cn/java/j-lo-jaxrs/


1. Define a User Object

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. Write Service Interface

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)  //Specify the type
@Consumes (mediatype.text_xml) returned to the client  //Receive client type 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, the Realization service class

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 to 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 to 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, writing Client

You can enter Http://localhost:9999/user/info/3?name=is_zhoufeng access directly in the browser. The following results are output in the browser:

<user><birthday>1987-02-01t00:00:00+08:00</birthday><id>3</id><name>is_ Zhoufeng</name></user>


(the corresponding user type should be generated on the client before the following action.) The corresponding XSD file can be provided on the server side. Generated to the client. I just copied the user over here.

First write the client service interface based on the service side

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);





Access Get method

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 ());   
	}   


Accessing Post methods

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 Classpathxmlap Plicationcontext (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 ());
	}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.