创建一个RESTful WebService并由Glassfish 4提供服务 [英] Create a RESTful WebService and serve it by Glassfish 4

查看:109
本文介绍了创建一个RESTful WebService并由Glassfish 4提供服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JEE6中看到了很多关于RESTful WebServices的问题,所以我想与您分享这个示例解决方案,这表明您可以轻松实现RESTful Web服务。

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.

首先创建一个新的Dynamic Web Project并将Glassfish 4添加为新的运行时,然后按照此文本。

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

推荐答案

在我们的示例应用程序我们有一个名为Item的实体类,它包含名称,价格和金额。在这个例子中,我们没有数据库,所以我们没有@Entity Annotation。

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:

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;
    }
}

现在我们需要实现一个SessionBean,它可以完成所有操作我们的业务逻辑是什么,所以这里是ItemsBean:

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;
    }
}

为glassfish提供启动泽西岛提供RESTful的建议我们需要创建一个配置类的WebService功能,这里是:

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 {

}

最后但并非最不重要的是ItemService本人,他们有一些方法我们可以从外部打电话以获得不同的资源演示..

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();
    }
}

这是设置RESTful的一种非常好的方法Webservice我认为,前者,当我使用Glassfish 3时,到目前为止我还使用glassfish 3,这还有一些工作要做,所以我认为使用glassfish 4和Java 7它现在变得更加容易了。

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.

你可以在上面找到示例代码GitHub: https://github.com/PatrickOtt/RESTful_WS_Example

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

这篇关于创建一个RESTful WebService并由Glassfish 4提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆