【问题标题】:Create a RESTful WebService and serve it by Glassfish 4创建一个 RESTful WebService 并由 Glassfish 4 提供服务
【发布时间】:2013-12-03 21:24:56
【问题描述】:

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

首先创建一个新的动态 Web 项目并将 Glassfish 4 添加为新的运行时,然后按照此文本进行操作。

【问题讨论】:

    标签: java web-services rest glassfish


    【解决方案1】:

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

    类项目:

    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:

    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 启动 Jersey 以提供 RESTful WebService 功能,我们需要创建一个“配置类”,这里是:

    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 本人,他有一些我们可以从外部调用的方法来获得不同的资源表示......

    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 Web 服务的一种非常好的方式,以前,当我使用 Glassfish 3 时,到现在我也使用 glassfish 3,而且还有一些工作要做,所以我认为使用 glassfish 4 和 Java 7 现在变得更容易了。

    我希望这对某人有所帮助,我在大学练习中以此为例。

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

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多