Create a RESTful WebService and serve it by Glassf

2020-07-24 07:31发布

问题:

I saw a lot of questions about RESTful WebServices in JEE6, so i wanted to share this example solution with you, which show how easy you can implemet a RESTful Webservice.

At first create a new Dynamic Web Project and add Glassfish 4 as new runtime, then follow this text.

回答1:

In our example application we have a entity class called Item, which holds a name, price and amount. In this example we do not have a database behind, so we have no @Entity Annotation.

Class Item:

package de.professional_webworkx.model.entities;

public class Item {

    private String name;
    private double price;
    private int count;

    public Item() {
        super();
    }

    public Item(String name, double price, int count) {
        super();
        this.name = name;
        this.price = price;
        this.count = count;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

Now we need to implement a SessionBean, which does all the business logic stuff for us, so here is the ItemsBean:

package de.professional_webworkx.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

import de.professional_webworkx.model.entities.Item;

/**
 * this is our stateless session bean which does all the database work,
 * e.g. send queries to the database and give results back to the calling
 * class(es)...
 * @author ottp
 *
 */
// in this SessionBean you can implement your business logic and 
// fill a list or a object and give it to the webservice class by 
// getter methods...
@Stateless
public class ItemsBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 5684254200888793061L;
    private List<Item> items = new ArrayList<Item>();

    public ItemsBean() {

    }

    @PostConstruct
    public void init() {
        for(int i = 0; i < 1000; ++i) {
            items.add(new Item("Item_"+i, Math.random()*i, (int) i*i));
        }
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }
}

To give glassfish the advise to initiate Jersey to provide RESTful WebService features we need to create a "Configuration class", here it is:

package de.professional_webworkx.ws.config;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

// under this ApplicationPath Glassfish will 
// load the WebService
@ApplicationPath("/REST")
public class WSConfiguration extends Application {

}

And last but not least the ItemService himself, who has some methods which we can call from outside to get different resource presentations..

package de.professional_webworkx.ws.resources;

import java.util.List;

import javax.ejb.EJB;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import de.professional_webworkx.business.ItemsBean;
import de.professional_webworkx.model.entities.Item;

// this is the path for the items service class,
// here we will get some information about 
// the entity Item
@Path("/items")
public class ItemsService {

    @EJB
    private ItemsBean bean;

    @Path("/info")
    @GET
    public String info() {
        return "Welcome to the ItemsService";
    }

    @Path("/all")
    @GET
    @Produces("application/json")
    public List<Item> getAllItems() {
        return bean.getItems();
    }
}

This is a very nice way to set up a RESTful Webservice i think, former, when i used Glassfish 3, up to now i also use glassfish 3, and it is a little more work to do, so i think with glassfish 4 and Java 7 it become more easier now.

I hope this helps someone, i used this as a example in a uiversity exercise.

You can find the example code on GitHub: https://github.com/PatrickOtt/RESTful_WS_Example