dimanche 10 octobre 2010

RESTful client in Java

Because JavaFX 2.0 will be a Java API, we shall have to use Java (or Groovy, Scala, JRuby,etc ... but this isn't the purpose  of this post) instead of JavaFX Script for use it.
And because, the news UIs creates with JavaFX 2.0 will have to consume RESTtful JSON services, I wrote a small RESTful client in Java.
To do that, I use the JAXB's implementation for JSON from
Jersey and for the RESTful service to be consume, the RESTful service wrote in an old post: RESTful service with JAX-RS (and JPA 2 for the access to the data)
If I compare with the client that I had written in JavaFX Script, it's easier to write a RESTFul client in Java/Jersey than in JavaFX Script because we don't have to write a parser (JavaFX' PullParser).
But, in Java, the beans are more verbose… (getter and setter)
Let's go for the code!
We have 2 Beans:
  • ListMusic, which maps the JSON array which is returned when calling the RESTful service.
  • Music, which maps one element of the JSON array
And the RestfulClient wich calls the RESTful service and parses the JSON result with Jersey.


RestfulClient.java
package restfulclient;

import com.sun.jersey.api.json.JSONJAXBContext;
import com.sun.jersey.api.json.JSONUnmarshaller;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestfulClient {

       public static void main(String[] args) {

        try {
            URL url = new URL("http://localhost:8080/RESTfulServices/rs/ArtisteNameBeginningBy/Arc");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();

            InputStream responseStream = connection.getInputStream();

            JSONJAXBContext jc = new JSONJAXBContext(ListMusic.class);
            JSONUnmarshaller u = jc.createJSONUnmarshaller();

            ListMusic m = u.unmarshalFromJSON(responseStream,ListMusic.class);
            connection.disconnect();

            System.out.println(m.getMusic());

        }
        catch(java.net.MalformedURLException mue){
            mue.printStackTrace();
        }
        catch(java.io.IOException ioe){
            ioe.printStackTrace();
        }
        catch(javax.xml.bind.JAXBException jaxbe){
            jaxbe.printStackTrace();
        }
    }
}

ListMusic.java
package restfulclient;

import java.util.ArrayList;
import java.util.List;

public class ListMusic {

    List<Music> music = new ArrayList();

    public List<Music> getMusic() {
        return music;
    }

    public void setMusic(List<Music> music) {
        this.music = music;
    }
    

}

Music.java
package restfulclient;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Music implements Serializable {

    private Long id;
    private String artisteName;
    private String albumTitle;

    public Music() {
    }

    public String getAlbumTitle() {
        return albumTitle;
    }

    public void setAlbumTitle(String albumTitle) {
        this.albumTitle = albumTitle;
    }

    public String getArtisteName() {
        return artisteName;
    }

    public void setArtisteName(String artisteName) {
        this.artisteName = artisteName;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString(){

        StringBuilder sb = new StringBuilder();
        sb.append("id : ");sb.append(id);sb.append(" ; ");
        sb.append("artisteName : ");sb.append(artisteName);sb.append(" ; ");
        sb.append("albumTitle : ");sb.append(albumTitle);
        sb.append(" \n");

        return sb.toString();
    }

}

Aucun commentaire: