Package Com.bullshit.webcrawler.client.impl;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Java.util.list;import Com.bullshit.storage.entity.authuser;import Com.bullshit.storage.entity.proxy;import Com.bullshit.storage.entity.vo.authusersvo;import Com.fasterxml.jackson.databind.objectmapper;public Class Proxycollectionrestservice {private static final String TargetUrl = "http://localhost:8080/blueberry/rest/v1/api/ Proxies ";p ublic proxy[] getallproxies () {proxy[] proxies = null;try {URL restserviceurl = new URL (targeturl); HttpURLConnection httpconnection = (httpurlconnection) restserviceurl.openconnection (); Httpconnection.setrequestmethod ("GET"); Httpconnection.setrequestproperty ("Accept", "Application/json"); if (Httpconnection.getresponsecode ()! =) {throw new RunTimeexception ("HTTP GET Request Failed with Error code:" + Httpconnection.getresponsecode ()); } BufferedReader Responsebuffer = new BufferedReader (New InputStreamReader (httpconnection.getinput Stream ()))); String output; Objectmapper objectmapper = new Objectmapper (); while (output = Responsebuffer.readline ()) = null) {proxies = Objectmapper.readvalue (output, proxy[].class); list<proxy> myObjects22 = objectmapper.readvalue (output, Objectmapper.gettypefactory (). Constructcollectionty PE (List.class, Proxy.class)); System.out.println (Myobjects22.size ()); Break } httpconnection.disconnect (); } catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }return proxies;} Public Authusersvo Postauthuserinfo (Authuser record) {AUTHUSERSVO AUTHUSERSVO = null; try {URL TargetuRL = new URL (targeturl); HttpURLConnection httpconnection = (httpurlconnection) targeturl.openconnection (); Httpconnection.setdooutput (TRUE); Httpconnection.setrequestmethod ("POST"); Httpconnection.setrequestproperty ("Content-type", "Application/json"); Objectmapper objectmapper = new Objectmapper (); String input = objectmapper.writevalueasstring (record); OutputStream outputstream = Httpconnection.getoutputstream (); Outputstream.write (Input.getbytes ()); Outputstream.flush (); if (Httpconnection.getresponsecode ()! =) {throw new RuntimeException ("Failed:http Error code:" + Httpconnection.getresponsecode ()); } BufferedReader Responsebuffer = new BufferedReader (New InputStreamReader ( (Httpconnection.getinputstream ()))); String output; System.out.println ("Output from server:\n"); while (output = Responsebuffer.readline ()) = null) {System.out.println (output); } httpconnection.disconnect (); } catch (Malformedurlexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); }return AUTHUSERSVO;} public static void Main (string[] args) {Proxycollectionrestservice service = new Proxycollectionrestservice (); Proxy[] s = service.getallproxies (); System.out.println (s.length);}}
Java calls restful WebService & Jackson