Build.gradle:
Compile group: ' Org.codehaus.jackson ', Name: ' JACKSON-MAPPER-LGPL ', version: ' 1.9.13 '
Compile group: ' Org.codehaus.jackson ', Name: ' JACKSON-CORE-LGPL ', version: ' 1.0.1 '
Compile group: ' HttpUnit ', Name: ' HttpUnit ', Version: ' 1.3 '
Send WebRequest related packages, such as the Jackson Bag and webconversation/webresponse, respectively. Click this refresh to add a reference.
Call request in Main
// interface of the API //Normalservice is the deserialization class Normalservice = (normalservice) testused to receive the API . Callresetapi (Class.forName ("Normalservice"), URL, "GET");
Below is the CALLRESETAPI section, which uses generics in Java, and generics for Java refer to http://www.cnblogs.com/iyangyuan/archive/2013/04/09/3011274.html
Import Com.meterware.httpunit.webconversation;import Com.meterware.httpunit.webresponse;import org.codehaus.jackson.map.objectmapper;/** * Created by Ygshen on 2015/3/3. */public class Test {public static <T> T Callresetapi (class<t> tclass, final string url, string method) T Hrows Exception { webconversation conversation = new Webconversation (); WebResponse response = conversation.getresponse (URL); String Respstream = Response.gettext (); Return ReadObject ( tclass, respstream); } private static <T> T ReadObject ( class<t> tclass, String stream) throws Exception { t data = tCl Ass.newinstance (); try { data = new Objectmapper (). ReadValue (Stream,tclass); } catch (Exception e) { //Handle the PROBLEM
throw e; } return data;} }
The following is the deserialized class code
Import Java.io.serializable;import java.util.list;/** * Created by Ygshen on 2015/3/3. */public class Normalservice implements serializable{public list<service> servicelist; public int count; Public String env; public Boolean success;} Service section Code import java.io.serializable;import java.util.list;/** * Created by Ygshen on 2015/3/3. */public class Service implements serializable{public String Servicecode; Public String serviceName; Public String servicenamespace; Public String servicecontacts; Public list<serviceendpoint> endpointlist;} /** * Created by Ygshen on 2015/3/3. */public class ServiceEndpoint {public String Test1; Public String Test2; Public String Test3;}
This allows you to test the output in the main function as soon as possible.
Using Jackson to deserialize in Java