I am new to restlet development. I want to develop an application using restlet which takes input as xml and returns xml as response.
I could not find it on official RESTLET website. Please someone guide how to proceed on this.
Thanks & Regards
I am not familiar with RESTLET, but according to their documentation page here: http://restlet.com/learn/tutorial/2.2/#part03
it is really easy to get XML in and out. XML is a simple String which follows some conventions. In the get method, just receive xml as String, parse it (ie. with SAXParser), get necessary information. And return value should also be String. Just make the XML String yourself and return it from the #Get annotated method as in example.
Related
I am currently trying to build a RESTful API using raw JAX-RS. I have learned that when building REST APIs, there is the principle called HATEOAS(Hypermedia as the engine of application state). In my class we used Link Headers to tell the client, how to further progress the application. I have managed to implement all basic functionality and can access the server after deploying the application to a tomcat server.
My question now is, how do I add a header-link that contains a wildcard for the user to fill in, for example an id?
So far I have tried
#Path("/resources")
#Produces(MediaType.APPLICATION_JSON)
public Response listAllResources()
{
List<TestResource> resources = ...
// get stuff from database
return Response.ok(resources)
.link(UriInfo.getAbsolutePathBuilder().path("{id}").build(), "edit")
.build;
}
After I try to access the above defined path, I get an error message that the template variable id is undefined.
I can't find any helpful resource that shows me how to create a link header that looks like:
link: <http://example.com/api/resources/{id}>; rel: "edit"
I hope my question was clear enough since this is my first question on stackoverflow :)
Thanks in advance!
I found out, that links like in my example http://example.com/api/resources/{id} aren't possible by JAX-RS because the UriBuilder tries to resolve any URL part that's surrounded by curly braces. So just use like http://example.com/api/resources/:id, if you want to give an Uri Template. Unfortunately the client then has to do something like a String.replace() to actually "create" a valid URI.
I have an Angular Controller, a factory service and a app.js.
There is also a MongoDB in the background. So i have a GET Method and it's already running very well. But how do i create a post method over HTTP?
So there is a formular as a html file. There are some CheckBoxes and input fields. AngularJS i use for the Frontend, in the background theres a JAVA Program.
You can use the below shorthand syntax for GET and POST requests:
GET: $http.get('/URL').success(successCallback);
POST: $http.post('/URL', data).success(successCallback);
Refer to this link for details on how to set different parameters. You may also have a look at tutorials here.
How can i pass a complex object like Person class as an input parameter in restful web service??
I have gone through many links but didnt find the solution. Please help
Transform the object into JSON or XML (or any other format, but these are the most common), and send it into the body of the request.
There are Restful Client api's available in java
1.JAX-RS-API-->JersyImpl,Resteasy(Jboss) Impl etc
You can use any of these and can pass any java object as input to restful resource.
I am trying to implement the example shown here.
The example seems to be for a jersey setup, which I am not using or familiar. How hard would it be to convert this to a standard java servlet project(idk how to name this)
What steps should I take. It seems most of the # annotations need to be changed to servlets.
This also seems very differnt from the standard appengine upload setup which all takes place in one servlet.
This would be a lot of work to rework the code to standard servlet and remove jersey. Jersey takes away so much boilerplate code. For example the JSON conversion is done by jersey, which otherwise would have to be custom implemented.
And you can for sure deploy more than one servlet to gae, in which way should this be standard?
Just look at the first method:
#GET
#Path("/url")
public Response getCallbackUrl() {
String url = blobstoreService.createUploadUrl("/rest/file");
return Response.ok(new FileUrl(url)).build();
}
When using only standard servlet you would need to do:
Servlet Definition and Mapping in web.xml to /url
Implement a HttpServlet, override doGet() method
Send Response Code 200 OK
Set appropriate HTTP Response Headers
Convert Response to JSON and write it to response
I am creating a simple RESTful web service with simple types successfully. Now I want to pass an object as argument for web service and get the object as response. My scenario is, Parse the XML message as object by using Jaxb and send the object as request for web service. After that in server side it process the requested object and generates the response xml file and send back it as object.
In URL path i give
"http://localhost:8080/SampleWS/rest/checkXML/username=visolve&password=visolve"
for simple type. But in object I don't know how to give the object reference in URL. Please help me how to solve my problem..
Regards
Bathakarai
Just define a very good-looking domain object. JAXB and JAX-RS will do the rest.
JAXB.
#XmlRootElement
class Regards {
#XmlElement
private long sincerely;
}
JAX-RS.
#Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
#POST
#Path("/sincerely")
#Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response sincerely(final Regards regards) {
regards.setSincerely(System.currentTimeMillis());
return Response.ok(regards).build();
}
Though you could certainly include the entire XML content in your URL, I would probably shy away from it.
Think of it this way: if you encode the XML in the URL you're potentially adding more work on both ends. Now the server and client will both need to know how to build the URL properly, and check to make sure everything lines up correctly. What if, in the future, you need to offer a JSON or YAML view of the same content? Now your URL might need to include the content-type as well. What about character-encoding?
All this to say, HTTP provides a terrific transport mechanism which already addresses these concerns. Include the XML as the entity body of the HTTP message, and use the HTTP header to identify what content-type you're sending, character-encoding, etc. This will work both ways (the server and client both can send the XML back/forth), and makes better use of HTTP.
Here's a related link which might help with some of the details. And another.
On a side note, please, please, please tell me you don't plan on sending user-credentials in plain text across an unencrypted link.