I'm searching a nice way to send a java object to my rest web service.
It's possible or not ?
For sample I wan't to send an "User" Object to my rest :
public Class User{
private String name;
private String surname;
public getName(){
return name;
}
public setName(String name){
[...]
}
It's possible to generate AUTOMATICALY this kind of Rest ?
www.foo.com/createUser/name="foo"&surname="foo"
I would consider using a JSON representation for this kind of Java objects.
I prefer the Jersey implementation of JAX-RS and it has built-in support for JSON serialization over JAXB.
Hope this helps...
Have a look at Restlet. The tutorial shows you how to get started.
Restlet allows you to use a number of representation formats, including XML and JSON.
It's possible to generate AUTOMATICALY this kind of Rest ?
www.foo.com/createUser/name="foo"&surname="foo"
That's NOT REST. That's RPC.
Related
assume that I have class like this
public class Entity {
private String name;
private File file;
// Getters & setters
}
is it possible to return object of this class as json? I'm using jax-rs
Yes, you can.
If you're using JAX-RS, then I expect you to know the basic of creating a REST-ful Web Services ya.
You can use #Produces("application/json") annotation in one of your endpoint. Use that endpoint to return an Entity, and it will be automatically converted as JSON.
I am looking to get the key and value to each Json formatted call and use them as java objects such as String or Integer ,in a rest client i would enter
{
"Name":"HelloWorld"
}
And i would get back the HelloWorld mapped to its Key so far ive seen examples but im just having trouble finding out what each tag does and how to parse the body to give the above results
#POST
#Path("/SetFeeds")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
#JsonCreator
public String setFeed(String jsonBody,#Context UriInfo uriInfo){
// Code to manipulate the body of the request
return response;
}
First thing you need to understand is how request body parsing is done. In JAX-RS parsing (or unmarshalling/deserializing/whatever) is done with MessageBodyReaders. There are different readers that can handle different Content-Type. For instance if you have Content-Type application/octet-stream, there is a reader that will unmarshal to byte[] or File or InputStream. So the following would work out the box
#Consumes("application/octet-stream")
public Response post(File file) {} // or `byte[]` or `InputStream`
That being said, JAX-RS implementations come with very basic readers for "easily convertible" format. For example, most requests can be converted to String, so you get that free for most Content-Types, as you are with your current code.
If we want some more complex data types, like your HelloWorld for Content-Type application/json, there is no standard reader for this. For this to work, we either need to create our own reader or use a library that comes with a reader. Luckily, the most popular JSON library in Java, Jackson, has implemented a JAX-RS provider that has a reader and a writer (for serialization).
Now depending on what server/JAX-RS implementation you are using, different implementations create light wrappers around the core Jackson JAX-RS module. If I knew the JAX-RS implementation you were using, I could recommend which wrapper to use, or you can forget the wrapper and just go with the basic Jackson module, which is
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
The above is a Maven dependency. If you are not using Maven, then basically you need to download all these jars.
You can find all of them here. Just search for them individually.
Then you need to register the provider. Again it depends on your JAX-RS implementation and how you are handling the configuration of your resource classes. I would need to see your application configuration (either web.xml or Java code) and maybe the server you are using to help with that. For the most part, the JacksonJsonProvider (which is the reader and writer) needs to be registered.
Once you have it registered then you need to understand the basics of how Jackson handles the serialization. At most basic level, Jackson looks for JavaBean properties (basic getter/setter) to match with JSON properties. For instance, if you have this bean property
public class HelloWorld {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The JSON should look like {"name": "whatever"}. The "name" key is the same as the bean property. In Bean property terms, the name of the property is all letters after the get/set with the first letter lowercased.
That's pretty much all there is to it. Now you can do
#Consumes("application/json")
public Response post(HelloWorld helloWorld) {
String name = helloWorld.getName(); // should == "whatever"
return Response.ok(helloWorld).build(); // we can also return objects
}
For more complex JSON formats, you should refer to the Jackson documentation or ask a question here on SO.
As far as the registering of the JacksonJsonProvider, if you are having trouble, please provide the information I requested, i.e. application configuration (web.xml or Java config) and the server you are using.
See Also:
JAX-RS Entity Provider to learn more about readers and writers
I want to upload a list of objects to my webserver through a REST API. I'm not sure if it's possible to do it with a REST call?
The object looks like this:
class Position {
private Date date;
private Double latitude;
private Double longitude;
}
Can I do it with something like this:
http://www.example.com/positions?userId=abc&position1=position1&position2=position2 ?
Or should I create a JSON representation of it and upload/put that to the webserver?
You should be able to do this using the Jersey REST framework. It might be enough just to enable the POJO mapping feature.
We do something similar in our project, where we marshal Java data types into JSON, and return that in our HTTP response.
Use http POST instead of GET. You don't have to use json.
I do not want an SDK with Facebook specific methods because I am programming in a REST style using JAX-RS 2.0 Client (Jersey Client).
I find myself creating classes such as:
#JsonIgnoreProperties(ignoreUnknown = true)
#XmlRootElement
public class User {
public String id;
public String name;
public String toString () {
return "id: " + id + "; name:" + name;
}
}
Is there such a set of model classes maintained by somebody without the overhead of an SDK. I do not need a library that includes a client and custom methods for getting friends, etc. or that store credentials. I am perfectly happy with Jersey. All I need is model classes.
Any pointers? It is OK if there is an SDK that has a clean set of model classes that I could use without having to do anything with the rest of the SDK (pun unintended).
I wrote a Java wrapper for Facebook's REST Graph API (http calls to the graph api, and translations from json objects to java classes..). You can check it out on github:
https://github.com/itzikrou/fishbowl
Hope it's useful.
I have a RESTful web services , up and running and it produces XML output upon a call by the client.
So I have a class to represent the data and I have annotated with #XMLRootElemnt and so it produces the data accordingly as XML. My question is - what is the best way to represent the XML , when there is an inner class in the class that I have annotated with #XMLRootElement? Pardon me if the question is not clear, and if you ask me more detail, I can explain. But if someone already got what I am asking, please advise.
I think that you want to explain that you want to produce an XML like this:
<programmer>
<name>Marcelo Tataje</name>
<pl>Java</pl>
<id>1</id>
</programmer>
And you have used something like this to produce it:
#XmlRootElement
public class Programmer
{
private String name;
private String pl;
private int id;
public Programmer()
{
}
//here your getters and setters
}
And your web services looks like this:
#GET
#Produces(MediaType.APPLICATION_XML)
#Path("/getProgrammer")
public Programmer getProgrammer()
{
Programmer p = new Programmer();
p.setName("Marcelo Tataje");
p.setPl("Java");
p.setId(1);
return p;
}
Ant then you invoke your client and so on... I think that's the simpliest way to do it and it's not bad, if you want the best way or a better method it is by using Spring3 which supports Rest and XML, you will have a structure for your requirements in a flash, is a faster framework. Recommended, well I'm answering to you of what I understand to your question.
http://blog.springsource.com/2009/03/08/rest-in-spring-3-mvc/
Got it worked. basically need to use the #XmlElementWrapper and #XmlElement and the field property will be a list of string.