Is there an easy way to return data to web service clients in JSON using java? I'm fine with servlets, spring, etc.
JSON Web Services let you access portal service methods by exposing them as a JSON HTTP API. Service methods are made easily accessible using HTTP requests, both from JavaScript within the portal and from any JSON-speaking client.
To post JSON to a REST API endpoint using Java, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the Java POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.
To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.
It might be worth looking into Jersey. Jersey makes it easy to expose restful web services as xml and/or JSON.
An example... start with a simple class
@XmlType(name = "", propOrder = { "id", "text" }) @XmlRootElement(name = "blah") public class Blah implements Serializable { private Integer id; private String text; public Blah(Integer id, String text) { this.id = id; this.text = text; } @XmlElement public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @XmlElement public String getText() { return text; } public void setText(String value) { this.text = value; } }
Then create a Resource
@Path("/blah") public class BlahResource { private Set<Blah> blahs = new HashSet<Blah>(); @Context private UriInfo context; public BlahResource() { blahs.add(new Blah(1, "blah the first")); blahs.add(new Blah(2, "blah the second")); } @GET @Path("/{id}") @ProduceMime({"application/json", "application/xml"}) public Blah getBlah(@PathParam("id") Integer id) { for (Blah blah : blahs) { if (blah.getId().equals(id)) { return blah; } } throw new NotFoundException("not found"); } }
and expose it. There are many ways to do this, such as by using Jersey's ServletContainer. (web.xml)
<servlet> <servlet-name>jersey</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
Thats all you need to do... pop open your browser and browse to http://localhost/blah/1. By default you will see XML output. If you are using FireFox, install TamperData and change your accept
header to application/json
to see the JSON output.
Obviously there is much more to it, but Jersey makes all that stuff quite easy.
Good luck!
We have been using Flexjson for converting Java objects to JSON and have found it very easy to use. http://flexjson.sourceforge.net
Here are some examples:
public String searchCars() { List<Car> cars = carsService.getCars(manufacturerId); return new JSONSerializer().serialize(cars); }
It has some cool features such as deepSerialize to send the entire graph and it doesn't break with bi directional relationships.
new JSONSerializer().deepSerialize(user);
Formatting dates on the server side is often handy too
new JSONSerializer().transform( new DateTransformer("dd/MM/yyyy"),"startDate","endDate" ).serialize(contract);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With