View Javadoc

1   package at.meikel.dmrl.client.portlet;
2   
3   import javax.ws.rs.core.MediaType;
4   
5   import net.sf.json.JSONArray;
6   import net.sf.json.JSONObject;
7   
8   import com.sun.jersey.api.client.Client;
9   import com.sun.jersey.api.client.WebResource;
10  
11  public class JerseyClient {
12  
13  	// See:
14  	// http://jersey.java.net/nonav/documentation/latest/user-guide.html
15  	// http://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with
16  
17  	public static void main(String[] args) {
18  		// /players/byLicenseId/37225
19  		// /playersByTeam/SG%20Weiterstadt%201886
20  		// /players
21  
22  		String[] services = new String[] { "/players/byLicenseId/37225",
23  				"/playersByTeam/SG%20Weiterstadt%201886"/* , "/players" */};
24  
25  		Client c = Client.create();
26  		WebResource r = c.resource("http://dmrl.meikel.cloudbees.net:80/rest");
27  
28  		for (String path : services) {
29  			String response = r.path(path).accept(
30  					MediaType.APPLICATION_JSON_TYPE).get(String.class);
31  			System.out
32  					.println("==================================================");
33  			System.out.println(path);
34  			System.out
35  					.println("==================================================");
36  			System.out.println(response);
37  			try {
38  				JSONObject json = JSONObject.fromObject(response);
39  				System.out.println(json);
40  			} catch (Exception e1) {
41  				try {
42  					JSONArray json = JSONArray.fromObject(response);
43  					System.out.println(json);
44  				} catch (Exception e2) {
45  					// ignore
46  				}
47  			}
48  
49  			System.out.println();
50  			System.out.println();
51  			System.out.println();
52  			System.out.println();
53  			System.out.println();
54  		}
55  
56  		// String path = services[0];
57  		// String response =
58  		// r.path(path).accept(MediaType.APPLICATION_JSON_TYPE)
59  		// .get(String.class);
60  		// JSONObject json = JSONObject.fromObject(response);
61  		// System.out.println(json);
62  		// System.out.println(json.get("vorname"));
63  	}
64  }