除了本身进行HTTP请求(甚至可以仅使用 **java.net.URL.openConnection**
即可完成)之外,您还需要一个JSON库。为了方便地与POJO绑定,我建议使用Jackson。
因此,类似:
// First open URL connection (using JDK; similar with other libs)URL url = new URL("http://somesite.com/requestEndPoint");URLConnection connection = url.openConnection();connection.setDoInput(true); connection.setDoOutput(true); // and other configuration if you want, timeouts etc// then send JSON requestRequestObject request = ...; // POJO with getters or public fieldsObjectMapper mapper = new ObjectMapper(); // from org.preahaus.jackson.mapmapper.writevalue(connection.getOutputStream(), request);// and read responseResponseObject response = mapper.readValue(connection.getInputStream(), ResponseObject.class);(显然具有更好的错误检查等)。
有更好的方法可以使用现有的rest-client库;但从低层次看,这仅是HTTP连接处理以及与JSON进行数据绑定/从JSON进行数据绑定的问题。



