Ik ga een RESTful call maken in Java. Ik weet echter niet hoe ik de oproep moet doen. Moet ik de URLConnection gebruiken of andere? Kan iemand mij helpen. dank u.
Dit is erg ingewikkeld in java, daarom zou ik voorstellen om Spring's RestTemplate
abstractie te gebruiken:
String result =
restTemplate.getForObject(
"http://example.com/hotels/{hotel}/bookings/{booking}",
String.class,"42", "21"
);
Reference:
RestTemplate
Als je een eenvoudige oproep moet doen naar een REST service vanuit java gebruik je iets in deze trant
/*
* Stolen from http://xml.nig.ac.jp/tutorial/rest/index.html
* and http://www.dr-chuck.com/csev-blog/2007/09/calling-rest-web-services-from-java/
*/
import java.io.*;
import java.net.*;
public class Rest {
public static void main(String[] args) throws IOException {
URL url = new URL(INSERT_HERE_YOUR_URL);
String query = INSERT_HERE_YOUR_URL_PARAMETERS;
//make connection
URLConnection urlc = url.openConnection();
//use post mode
urlc.setDoOutput(true);
urlc.setAllowUserInteraction(false);
//send query
PrintStream ps = new PrintStream(urlc.getOutputStream());
ps.print(query);
ps.close();
//get result
BufferedReader br = new BufferedReader(new InputStreamReader(urlc
.getInputStream()));
String l = null;
while ((l=br.readLine())!=null) {
System.out.println(l);
}
br.close();
}
}
Er zijn verschillende RESTful API's in omloop. Ik zou Jersey aanbevelen;
Client API documentatie is hier;
https://jersey.java.net/documentation/latest/index.html
Bijwerken Locatie voor de OAuth docs in de opmerking hieronder is een dode link en is verplaatst naar https://jersey.java.net/nonav/documentation/latest/security.html#d0e12334