I am using Apache tomcat 6.0.20
I want to create Client To Consume RESTFul Web Service(using GET)
I know I can do it via the old fashion way with URLConnection (regular GET request).
But I wonder is there any way of doing it differently? maybe with Annotations?
I think this article http://www.oracle.com/technetwork/articles/javase/index-137171.html will give you good guidance how to act in both directions.
I'm currently using the API of spring. The connection handling for example is handled already within the RestTemplate class. Have a look to http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#rest-client-access.
Using NetBeans 7 there is the possibility to have RESTFul web services created with a simple wizard (with Jersey API): http://netbeans.org/kb/docs/websvc/rest.html . This approach uses annotations.
In the end I chose to use the JAVA SE API in the old and fashion way:
public void getRestfullMethod(...) throws IOException
{
String temp = null;
//Build the request data.
StringBuffer buf = new StringBuffer (..)
buf.append("&system=").append ("someVal");
String urlStr = buf.toString ();
//Send the request.
URL url = new URL (urlStr);
URLConnection con = url.openConnection();
//Return the response.
BufferedReader in = new BufferedReader (new InputStreamReader (con.getInputStream ()));
String inputLine = null;
buf = new StringBuffer ();
while ((inputLine = in.readLine ()) != null)
buf.append (inputLine);
in.close ();
}
Related
I'm trying to use http get request on java, to my localhost on port number 4567.
I got the get request: "wget -q -O- http://localhost:4567/XXXX"
[XXXX is some parameter - not relevent].
I've found a java library java.net.URLConnection for these kind of things but it seems that the URLConnection object is supposed to receive the url/port/.. and all other kind of parameters (In other words, you have to construct the object yourself), however, I got the full http get request as I've written above. Is there a way to simply 'shoot' the request without dealing with constructing the field for URLConnection?
You can create the URL object using your URL, it will figure out ports and other things itself. Ref : https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://localhost:4567/XXXX");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Why don't you use Apache HTTPClient library. It is simple to use.
HttpClient client = new HttpClient();
refer the document http://hc.apache.org/httpclient-3.x/tutorial.html
I'm programming android app as my final project. It's connecting to a java web-service on cloud.
My problem is that I want to use complex java objects and share them between my android app and my web service on cloud.
As example I have "Mission" class and i want use methods that get "Mission" type argument as parameter, or returns "Mission" type. I want to use those complex objects just as String, Integer or Boolean.
May I create a library or jar file or something that hold those classes on the client side and the server side?
What should I do to use those classes and complex java objects between the server-side and the client-side just as we use String or other regular java types?
The best method is using JSON. That it can not transfer all the data.
URL url = new URL("Your Web Service URL");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "");
}
// Append Server Response To Content String
Content = sb.toString();
sample :
http://androidexample.com/Restful_Webservice_Call_And_Get_And_Parse_JSON_Data-_Android_Example/index.php?view=article_discription&aid=101&aaid=123
I have a Java application that runs programs like Python on my computer like so:
Process p = Runtime.getRuntime().exec("C:/python34/pythonw.exe PWtest.pyw")
I need to deploy this on the web. Now is it possible to make this into a Java Web Start application such that when the app needs to run python, it accesses my server installed with python to generate data? Otherwise, how else can I do this?
NOTE: I don't want any solution that packages the python program or .exe into the .jar file because I have other programs like PowerWorld invoked and this app needs to be scalable.
You could pass the url of a server script such as .asp and read the result:
public static String excuteHTTPGet(final String targetURL) throws IOException {
final URL url = new URL(targetURL);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
// Get Response
final InputStream is = connection.getInputStream();
final BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
final StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
}
I am starting a tomcat server in my local for a web application and it takes around 20 minutes to be up and running. I want to check if the web app is up and running and taking any requests via java. Any help?
My server is say at localhost:8001/myapp
Thanks in advance.
You can check it through many ways. Like... Set a servlet as start up on-load and inside it keep some loggers which files log messages along with exact time.
You can add something like localhost:8001/myapp/status to the app that would return information about current status. Then you can just sent http request from java and check the response
public String execute(String uri) throws Exception {
URL url = new URL(uri);
URLConnection connection = url.openConnection();
connection.setReadTimeout(1000);
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
StringBuffer outputLine = new StringBuffer();
while ((inputLine = in.readLine()) != null)
outputLine.append(inputLine);
in.close();
return outputLine.toString();
}
I guess I will call this method after a certain time period to see if I'm getting a timeout exception of the raw html.
I am trying to grab a site's source code using this code
private static String getUrlSource(String url) throws IOException {
URL url = new URL(url);
URLConnection urlConn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
urlConn.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
When I do grab the site code this way I get an error about needing to allow cookies. Is there anyway to allow cookies in a java application just so I can grab some source code? I do have the cookie my browser uses to log me in if that helps.
Thanks
John
This way you would have to deal with raw request data, Go with apache http client that gives you abstraction and some methods to allow to set headers in request