Uncaught exception with 'DB connection error' on line 18

Howto RESTful Web Services with Netbeans and Tomcat from Scratch

1.
Select File->new Project->Java Web->Web Application.
Name the Project to RestServer or you alike.
Select Server Apache Tomcat and Java EE 5

restful_01

Finish.

2.
Create you data access objects, domains and services.
We leave the data access objects for later. We bind some data in the service class.

Create 2 new packages in the Source packages, e.g. Domains and services

Add the library Jersey 1.13 (JAX-RS RI) to the libraries for the project.

3. Create java class Address with getters and setters in the Domains package with the annotation @XmlRootElement.

@XmlRootElement
public class Address {
    private int id ;
    private String first_name ;
    private String last_name ;

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the first_name
     */
    public String getFirst_name() {
        return first_name;
    }

    /**
     * @param first_name the first_name to set
     */
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    /**
     * @return the last_name
     */
    public String getLast_name() {
        return last_name;
    }

    /**
     * @param last_name the last_name to set
     */
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
}

4. Create new class AddressServices in package services.


@Produces(MediaType.APPLICATION_XML)
@Path("/addresses")
public class AddressServices {

    @GET
    public List<Address> getAddressList() {
        List<Address> addressList =
               Collections.synchronizedList(new ArrayList<Address>()) ;

        // do some fake data
        Address address = new Address() ;
        address.setId(0);
        address.setFirst_name("First Name");
        address.setLast_name("Last Name");
        synchronized(addressList) {
            addressList.add(address);
        }
        address = new Address() ;
        address.setId(0);
        address.setFirst_name("John");
        address.setLast_name("Doe");
        synchronized(addressList) {
            addressList.add(address);
        }

        return addressList ;
    }

    @GET
    @Path("/{id}")
    public Address getAddress(@PathParam("id") int id) {
        Address address = getAddressList().get(id) ;
        return address ;
    }
}

Annotate the getAddressList function with the @GET annotation. When you save this file you will be asked to specify some registration stuff for REST resources. Leave the default settings and press OK.
restful_02
If you are not asked then you have to modify the web.xml by hand. Insert the following stuff:


Build the project. You will notice that an additional directory RESTful Web Services appears in the directory structure of the project.

You can set in the project properties the source format to JDK 7.

Right click on the RESTful Web Services to test the service or
http://localhost:8082/RestServer04/webresources/application.wadl
or
http://localhost:8082/RestServer04/webresources/addresses to see it RESTful.

http://localhost:8082/RestServer04/webresources/addresses/0 to see the id = 0.