code_junkie

Java REST client without schema

Nov 12th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import com.sun.jersey.api.client.*;
  2.  
  3. ...
  4.  
  5. ClientConfig clientConfig = new DefaultClientConfig();
  6. Client client = Client.create(clientConfig);
  7.  
  8. WebResource webResource = client.resource("https://hj.yahooapis.com/v1/HJAuthTokens");
  9. webResource.accept("application/xml");
  10.  
  11. // body is a hard-coded string, with replacements for the variable bits
  12. String response = webResource.post(String.class, body);
  13.  
  14. // parse response into a org.w3c.dom.Document
  15. // interface with Document via XPATH, or write my own POJO mappings
  16.  
  17. <?xml version="1.0" encoding="utf-8"?>
  18. <Response>
  19. <ResponseCode>0</ResponseCode>
  20. <ResponseMessage>Login successful</ResponseMessage>
  21. <Token>NTlEMTdFNjk3Qjg4NUJBNDA3MkJFOTI3NzJEMTdDNDU7bG9jYWxob3N0LmVnbGJwLmNvcnAueWFob28uY29tO0pVNWpzRGRhN3VhSS4yQVRqRi4wWE5jTWl0RHVVYzQyX3luYWd1TjIxaGx6U0lhTXN3LS07NjY2MzM1OzIzNDY3NTsxMjA5MDE2OTE5OzZCM1RBMVNudHdLbl9VdFFKMFEydWctLQ==</Token>
  22. </Response>
  23.  
  24. <?xml version="1.0" encoding="utf-8"?>
  25. <yahoo:error xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xml:lang="en-US">
  26. <yahoo:description>description</yahoo:description>
  27. <yahoo:detail>
  28. <ErrorCode>errorCode</ErrorCode>
  29. </yahoo:detail>
  30. </yahoo:error>
  31.  
  32. @XmlRootElement("Response")
  33. class Response {
  34. public int responseCode;
  35. public String responseMessage;
  36. public String token; // or perhaps byte[] works for automated base64?
  37. }
Add Comment
Please, Sign In to add comment