| View previous topic :: View next topic |
| Author |
Message |
handreach
Joined: 24 Feb 2009 Posts: 2
|
Posted: Tue Feb 24, 2009 7:09 am Post subject: Consume REST service with POST |
|
|
| I have successfully follow the tutorial of building RESTful web service using Netbeans 6. I could not, however, find a good tutorial teaching how to build a simple Java client application to consume the REST service, mainly the POST method. The reason is that I need the Java application to insert new records in database. Anyone has a good link to provide? Thanks in advance. |
|
| Back to top |
|
 |
handreach
Joined: 24 Feb 2009 Posts: 2
|
Posted: Wed Feb 25, 2009 7:30 pm Post subject: |
|
|
The following contains more details of my question:
I followed the Netbeans tutorial (http://www.netbeans.org/kb/docs/websvc/rest.html) to set up a simple RESTful webservice for a list of contact records stored in an Oracle database, and I've succeeded in calling the POST method of the RESTful web service from a web browser by pasting a XML formatted string into the web form. The POST method successfully inserted a new record in the Oracle table. Therefore, the server side seemed to be working fine. But when I tried to write a client code to POST the same XML string into the web service, Tomcat gave me an error of HTTP Status 415, i.e., wrong format.
I generated the client code by doing the "drag and drop" of the above POST web service into the client code's main function using Netbeans 6.5. The IDE generated a few lines of codes in the main function as well as some org.netbeans.saas.* classes in the source package. The added codes in the main function requested an InputStream variable named "content" as the input, i.e.,
| Code: | | RestResponse result = RootContactList.post(content); |
I supppse the InputStream variable is casted from an XML string, so here is the snippet that I used to generate the input to satisfy the post() function. I don't know why it failed, so please let me know if anything is wrong with it, and I'd greatly appreciate your time!
| Code: |
String xml =
"<contact>" +
"<contactId>11</contactId>" +
"<firstName>xxx</firstName>" +
"<lastName>yyy</lastName>" +
"</contact>";
ByteArrayInputStream content = new ByteArrayInputStream(xml.getBytes("UTF-8"));
RestResponse result = RootContactList.post(content);
|
The following is the error message in the output:
Feb 25, 2009 11:14:56 AM restclient.Main main
SEVERE: null
java.io.IOException: <html><head><title>Apache Tomcat/6.0.16 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 415 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.16</h3></body></html>
at org.netbeans.saas.RestConnection.connect(RestConnection.java:204)
at org.netbeans.saas.RestConnection.post(RestConnection.java:130)
at org.netbeans.saas.root.RootContactList.post(RootContactList.java:71)
at restclient.Main.main(Main.java:63)
BUILD SUCCESSFUL (total time: 1 second)
Thanks for your help! |
|
| Back to top |
|
 |
jpred
Joined: 14 May 2009 Posts: 2
|
Posted: Thu May 14, 2009 5:37 pm Post subject: some help |
|
|
hi , I'm trying to consume a Rest WS (GET & POST) from a java appplication, buy i cant find any tutorial ...
can you post your code ?
thanks |
|
| Back to top |
|
 |
Melongo Annabel Posted via mailing list.
|
Posted: Fri May 15, 2009 1:31 am Post subject: Consume REST service with POST |
|
|
Hopefully this helps: http://www.netbeans.org/kb/trails/java-ee.html
From: jpred <address-removed>
To: address-removed
Sent: Thursday, May 14, 2009 12:37:53 PM
Subject: [nbj2ee] Consume REST service with POST
hi , I'm trying to consume a Rest WS (GET & POST) from a java appplication, buy i cant find any tutorial ...
can you post your code ?
thanks |
|
| Back to top |
|
 |
michael Posted via mailing list.
|
Posted: Fri May 15, 2009 7:21 am Post subject: Consume REST service with POST |
|
|
Try here:
http://wikis.sun.com/display/Jersey/Main
Michael
-----Original Message-----
From: jpred [mailto:address-removed]
Sent: Thursday, May 14, 2009 8:38 PM
To: address-removed
Subject: [nbj2ee] Consume REST service with POST
hi , I'm trying to consume a Rest WS (GET & POST) from a java appplication, buy i cant find any tutorial ...
can you post your code ?
thanks |
|
| Back to top |
|
 |
jpred
Joined: 14 May 2009 Posts: 2
|
Posted: Fri May 15, 2009 2:00 pm Post subject: code |
|
|
thanks for your answer !
I saw the two links, I find a lots of example publish Rest WS, but nothing about consuming.
I trying to do something simple,
I publish this:
....
....
@GET
@Produces("text/html")
@Path("login/ci={ci}")
public String Login(@PathParam("ci") String ci){
if(ci != null){
doSomething();
}
else{
return "grrrr";
}
}
and now I want to consume from a java app.
thanks for you help! |
|
| Back to top |
|
 |
pavankumar
Joined: 03 Nov 2009 Posts: 1
|
Posted: Tue Nov 03, 2009 10:16 pm Post subject: |
|
|
| so any luck in finding the example for POST. |
|
| Back to top |
|
 |
|