应如何组织提供REST Web服务的JEE6企业应用程序? [英] How a JEE6 enterprise application that offers REST web services should be organized?

查看:105
本文介绍了应如何组织提供REST Web服务的JEE6企业应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从一个月前,我正在努力学习安静的网络服务。
现在我已经练习了语法,我理解这些概念,我决定做一个非常简单的企业应用程序,其中包括EJB,JPA和REST。
我正在努力了解组织这种系统的最佳方式是什么。如果有实习经验的人可以给我一些关于什么是最佳做法的技巧,我怎么可以解决我目前的问题呢?

Since a month ago i am studying restful web services really hard. Now that i did practice the Syntax and i understand the concepts, i decided to make a very simple enterprise application that includes EJB, JPA and REST. I am making a big effort in trying to understand what is the best way to organize this kind of system. Ill appreciate a lot if someone with experience in the field could give me some tips on what would be the best practice, and how can i solve my current problem.

请给我看看这张照片。对不起,我无法获得更好的分辨率(使用Ctrl +鼠标向上滚动缩放):

Let me show you this image please. Sorry i cannot get better resolution(Use Ctrl+ Mouse Scroll Up to zoom):

正如你所看到的,这是一个非常简单的企业应用程序,有2个模块。

As you can see this is a very simple enterprise app, that has 2 modules.

此应用程序不使用CDI(我想实现我的目标没有CDI帮助和)

This application does not use CDI( I want to achieve my goal without CDI help and)

当一些客户端(任何可互操作的客户端)发送一个带有一些参数的@GET,REST服务应将这些参数传递给EJB模块,该模块将在数据库中搜索并发回适当的数据。最后,服务将在JAXB的帮助下自动组织,并将.XML发送回客户端。

When some client(Any inter-operable client) sends a @GET with some parameters the REST service should pass those parameters to the EJB module which will search in the database and send back the appropiate data. At the end the service will automatically marshal with the help of JAXB and send the .XML back to the client.

我的问题如下:


  • 我得到一个ClassCastException,因为实体在EJB模块中与WebModule中的JAXB类不兼容(即使它们的变量相同)

  • 我不知道应该如何组织事物前端可以组织和解散那些实体。

  • 实体类应该在前端结合JAXB映射吗?如果那么,EJB模块就不会真的需要了。但是事情是,我想要EJB模块,因为我经常在那里执行我的CRUD操作。

  • 将EJB作为REST Web服务(混合)暴露?你认为这是一个好主意吗?怎么能帮助我?

  • 再次,如果我在Web模块中创建了JAXRS + EJB的混合,那么我将必须在前端创建我的JPA实体,这是我以前从未做过的事情。你认为这是一个很好的做法吗?

  • 你建议什么?通常使用REST Web服务的企业应用程序的组织方式是什么?

  • I get a ClassCastException because the entity in the that is in the EJB module is not compatible with the JAXB class in the WebModule(Even if their variables are the same)
  • I don't know how things should be organized so the front end can marshal and unmarshal those entities.
  • Should maybe the entity class be in the front end combined with the JAXB mapping? If then, there will not be really need for the EJB module. But the thing is, i want the EJB module because i often do my CRUD operations there.
  • What about exposing the EJB as a REST web service(making a hybrid)? Do you think this is a good idea? How can it help me?
  • Again if i create a hybrid of JAXRS+EJB in the web module, i will must create then my JPA entities in the front end and that is a thing i never did before. Do you think it is a good practice?
  • What do you suggest? What is often the way the Enterprise applications that use REST web services are organized?

推荐答案

以下是使用JPA实现为会话bean的JAX-RS服务的示例对于持久化和JAXB的消息传递可能看起来像。 (注意一个 EntityManager 注入会话bean,为什么要避免这种行为?):

Below is an example of a JAX-RS service implemented as a session bean using JPA for persistence and JAXB for messaging might look like. (note an EntityManager is injected onto the session bean, why do you want to avoid this kind of behaviour?):

package org.example;

import java.util.List;

import javax.ejb.*;
import javax.persistence.*;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Stateless
@LocalBean
@Path("/customers")
public class CustomerService {

    @PersistenceContext(unitName="CustomerService",
                        type=PersistenceContextType.TRANSACTION)
    EntityManager entityManager;

    @POST
    @Consumes(MediaType.APPLICATION_XML)
    public void create(Customer customer) {
        entityManager.persist(customer);
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("{id}")
    public Customer read(@PathParam("id") long id) {
        return entityManager.find(Customer.class, id);
    }

    @PUT
    @Consumes(MediaType.APPLICATION_XML)
    public void update(Customer customer) {
        entityManager.merge(customer);
    }

    @DELETE
    @Path("{id}")
    public void delete(@PathParam("id") long id) {
        Customer customer = read(id);
        if(null != customer) {
            entityManager.remove(customer);
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_XML)
    @Path("findCustomersByCity/{city}")
    public List<Customer> findCustomersByCity(@PathParam("city") String city) {
        Query query = entityManager.createNamedQuery("findCustomersByCity");
        query.setParameter("city", city);
        return query.getResultList();
    }

}

如果要使用相同的服务器端和客户端的域对象。然后我将通过XML提供JPA映射,而不是注释,以避免客户端上的classpath depedency。

If you want to use the same domain objects on the server and client side. Then I would provide the JPA mappings via XML rather than annotations to avoid a classpath depedency on the client.

更多信息

  • Part 1 - Data Model
  • Part 2 - JPA
  • Part 3 - JAXB (using MOXy)
  • Part 4 - RESTful Service (using an EJB session bean)
  • Part 5 - The client

更新

META-INF / persistence.xml

persistence.xml文件指定指向包含JPA映射的XML文件的链接:

The persistence.xml file is where you specify a link to the XML file that contains the JPA mappings:

<persistence-unit name="CustomerService" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>CustomerService</jta-data-source>
    <mapping-file>META-INF/orm.xml</mapping-file>
</persistence-unit>

META-INF / orm.xml

在此文件中,您将添加JPA元数据的XML表示。

It is in this file that you would add the XML representation of the JPA metadata.

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
    version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd">
    <entity class="org.example.Customer">
         <named-query name="findCustomersByCity">
            <query>SELECT c FROM Customer c WHERE c.address.city = :city</query>
         </named-query>
         <attributes>
            <id name="id"/>
            <basic name="firstName">
                <column name="FIRST_NAME"/>
            </basic>
            <basic name="lastName">
                <column name="LAST_NAME"/>
            </basic>
            <one-to-many name="phoneNumbers" mapped-by="customer">
                <cascade>
                    <cascade-all/>
                </cascade>
            </one-to-many>
            <one-to-one name="address" mapped-by="customer">
                <cascade>
                    <cascade-all/>
                </cascade>
            </one-to-one>
         </attributes>
    </entity>
    <entity class="org.example.Address">
        <attributes>
            <id name="id"/>
            <one-to-one name="customer">
                <primary-key-join-column/>
            </one-to-one>
        </attributes>
    </entity>
    <entity class="org.example.PhoneNumber">
        <table name="PHONE_NUMBER"/>
        <attributes>
            <id name="id"/>
            <many-to-one name="customer">
                <join-column name="ID_CUSTOMER"/>
            </many-to-one>
        </attributes>
    </entity>
</entity-mappings>

更多信息

  • Creating a RESTful Web Service - Part 2/5 (XML Metadata)

这篇关于应如何组织提供REST Web服务的JEE6企业应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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