Guest User

consometdsiWS

a guest
Apr 17th, 2015
17
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package consometdsiws;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import model.Marca;
  11.  
  12.  
  13. public class ConsometdsiWS {
  14.  
  15. private static final String targetURL =
  16. "http://localhost:8080/tdsiWS/webresources/tdsi/listarmarca";
  17.  
  18. public static void main(String[] args) throws MalformedURLException, IOException {
  19.  
  20.  
  21. URL restServiceURL = new URL(targetURL);
  22. HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
  23. httpConnection.setRequestMethod("GET");
  24. httpConnection.setRequestProperty("Accept", "application/json");
  25.  
  26. if (httpConnection.getResponseCode() != 200) {
  27. throw new RuntimeException("HTTP GET Request Failed with Error code : "
  28. + httpConnection.getResponseCode());
  29. }
  30.  
  31. BufferedReader responseBuffer =
  32. new BufferedReader(new InputStreamReader((httpConnection.getInputStream())));
  33.  
  34. String output;
  35. String json = new String();
  36.  
  37. while ((output = responseBuffer.readLine()) != null) {
  38. json = json + output;
  39. }
  40.  
  41.  
  42. ObjectMapper mapper = new ObjectMapper();
  43.  
  44. Marca marca;
  45.  
  46. marca = mapper.readValue(json, Marca.class);
  47.  
  48. httpConnection.disconnect();
  49.  
  50. }
  51.  
  52. }
RAW Paste Data