dien thoai di dong , dau gia , the gioi smartphone , download game mobile , smartphone , tang truong , khoa hoc cong nghe thong cong , mua ban sim , Smartphone gia re , cong nghe tuong lai , cong nghe 360 , giai tri guongmat.org , su kien trong ngay , thoi trang hi tech , thong tin 360 , may tinh bang , perfect body , kasuman.com , gia vang hom nay , tin tuc an toan , kinh te viet nam , xay dung viet nam , thoi trang , thoi trang , phu nu viet nam , tin tuc moi online , dich vu bao ve viet nam , bao ve viet nam , cong ty bao ve viet nam , tin tuc moi online , giai tri 24h , tin tuc 24h


 14 Dec 2009 @ 5:05 PM 

In this article I would like to describe how to get your RESTful webservice to output XML. RESTful doesnt output as much detail as the SOAP specification, but it gives you enough data to work with, assuming you know what the data types are.

Firstly, create a class that represents your output. Remember the import!

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class userData {
    public String firstname;
    public String lastname;
    public String idnumber;
    public String pin;
    public int status;
}

Then we can start coding the webservice. I’m not going to go into detail, but am just going to put in the specifics that are relevant for this article

import java.util.ArrayList;
import java.util.List;
 
@Path("airtime_functions")
public class airtime {
    @GET
    @Path("get_users")
    @Produces("application/xml")
    public List<userData> get_users(@QueryParam("user_id") String user_id)
    {
        List<userData> retUser = new ArrayList<userData>();
        try
        {
            errorMsg = "";
            con = getJNDIConnection();
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            String sql = "SELECT * from users where user_id = ?";
            PreparedStatement prest = con.prepareStatement(sql);
            prest.setString(1, user_id);
            prest.execute();
            rs = prest.getResultSet();
            while (rs.next())
            {
                userData toReturn = new userData();
                toReturn.firstname = rs.getString("firstname");
                toReturn.idnumber = rs.getString("idnumber");
                toReturn.lastname = rs.getString("surname");
                toReturn.pin = rs.getString("pin");
                toReturn.status = rs.getInt("status");
                retUser.add(toReturn);
            }
            con.close();
            return retUser;
        } catch (Exception e)
        {
            userData toReturn = new userData();
            toReturn.firstname = "Error: " + e;
            toReturn.idnumber = "";
            toReturn.lastname = "";
            toReturn.pin = "";
            toReturn.status = 0;
            retUser.add(toReturn);
            return retUser;
        }
    }

To access this resource we do it something like this http://localhost:8080/this_ws_name/airtime_functions/get_users?user_id=1

That should send an XML packet containing the data back to your client. From there it is up to you what you want to do with it

I hope this helps make RESTful a little bit simpler

Posted By: Zayin
Last Edit: 14 Dec 2009 @ 05:05 PM

EmailPermalink
Tags
Tags: , ,
Categories: java


 

Responses to this post » (2 Total)

 
  1. zayinkrige says:

    New Blog Post -> Create XML output with RESTful webservices in JAVA – http://www.zayinkrige.com/create-xml-out

  2. [...] This post was mentioned on Twitter by Zayin Krige, Zayin Krige. Zayin Krige said: New Blog Post -> Create XML output with RESTful webservices in JAVA – http://toast.tw/100cd1 [...]

Post a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


 Last 50 Posts
 Back
Change Theme...
  • Users » 1
  • Posts/Pages » 40
  • Comments » 73
Change Theme...
  • VoidVoid « Default
  • LifeLife
  • EarthEarth
  • WindWind
  • WaterWater
  • FireFire
  • LightLight

Links



    No Child Pages.

Portfolio



    No Child Pages.