1

我之前曾使用 Jaxb 处理过 Web 服务。我从 xsd 生成 Java,然后使用 HTTP post 将 xml 请求发布到指定的 URL。最近听说了这个Restful web services,读起来感觉之前做的只是restful web service。但是,我不确定它是否相同。谁能解释一下。

4

2 回答 2

0

听起来您一直在创建相同类型的 RESTful 服务。您可能指的是 JAX-RS,它是一种标准,它定义了一种创建 RESTful 服务的更简单方法,其中 JAXB 是application/xml媒体类型的标准绑定层。下面是一个示例服务:

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

}

了解更多信息

于 2012-08-13T01:31:40.583 回答
0

说到“RESTful”,它只是 HTTP 方法和 url 模式的约定。

CRUD   METHOD   URL                         RESPONSE DESCRIPTION
----------------------------------------------------------------
CREATE POST     http://www.doma.in/people   202      Creates a new person with given entity body
READ   GET      http://www.doma.in/people   200
READ   GET      http://www.doma.in/people/1 200 404  Reads a single person
UPDATE PUT      http://www.doma.in/people/2 204      Updates a single person with given entity body
DELETE DELETE   http://www.doma.in/people/1 204      Deletes a person mapped to given id(1)

您甚至可以使用 Sevlets 实现这类合同。实际上,在 JAX-RS.

当您使用JAX-RS.

这是 Blaise Doughan 先生的稍作修改的版本。Blaise Doughan 先生的代码没有任何问题。

我只想为上述 url 模式添加更多内容。

可以提供的一件很棒的事情JAX-RS是,如果您拥有那些优秀的 JAXB 类,您可以根据客户的需要提供 XML 和 JSON。以相同的方法查看这两种格式的@Producess 和@Consumess。

当客户端想要以 XML 格式接收时Accept: application/xml,他们只需获取 XML。

当客户端想要以 JSON 格式接收时Accept: application/json,他们只需获取 JSON。

@Path("/customers");
public class CustomersResource {

    /**
     * Reads all person units.
     */
    @POST
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response read() {
        final List<Customer> listed = customerBean.list();
        final Customers wrapped = Customers.newInstance(listed);
        return Response.ok(wrapped).build();
    }

    @POST
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response createCustomer(final Customer customer) {
        entityManager.persist(customer);
        return Response.created("/" + customer.getId()).build();
    }

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("/{id: \\d+}")
    public Response read(@PathParam("id") final long id) {
        final Customer customer = entityManager.find(Customer.class, id);
        if (customer == null) {
            return Response.status(Status.NOT_FOUND).build();
        }
        return Response.ok(customer).build();
    }

    @PUT
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void updateCustomer(final Customer customer) {
        entityManager.merge(customer);
    }

    @DELETE
    @Path("/{id: \\d+}")
    public void deleteCustomer(@PathParam("id") final long id) {
        final Customer customer = entityManager.find(Customer.class, id);
        if (customer != null) {
            entityManager.remove(customer);
        }
        return Response.status(Status.NO_CONTENT).build();
    }
}

假设您想提供一些图像?

@GET
@Path("/{id: \\d+}")
@Produces({"image/png", "image/jpeg"})
public Response readImage(
    @HeaderParam("Accept") String accept,
    @PathParam("id") final long id,
    @QueryParam("width") @DefaultValue("160") final int width,
    @QueryParam("height") @DefaultValue("160") final int height) {

    // get the image
    // resize the image
    // make a BufferedImage for accept(MIME type)
    // rewrite it to an byte[]
    return Response.ok(bytes).build();

    // you can event send as a streaming outout
    return Response.ok(new StreamingOutput(){...}).build();
}
于 2012-08-16T14:37:08.273 回答