RESTful Web服务——资源上下文和响应问题

3

我已经开发了一个简单的RESTful Web服务。

根资源类:

@Path("/order")
@RequestScoped
public class CustOrderContainerResource {

  //<editor-fold defaultstate="collapsed" desc="Instance Variable">
  @Context
  private UriInfo myUriInfo;

  @Context
  private ResourceContext myResourceContext;

  @Context
  private SecurityContext mySecurityContext;

  @Inject
  private CustOrderDAO myCustOrderDAO;

  public CustOrderContainerResource() {
  }


  @GET
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML})
  public List<Custorder> ReadCustomerOrder(@QueryParam("min")int min, 
      @QueryParam("max")int max, @Context Request myRequest, 
      @Context HttpHeaders myHeader) {

    int totalOrder = 0;
    List<Custorder> resultList = null;

    totalOrder = myCustOrderDAO.count();
    if(min == 0 && max == 0) {
      throw new QueryParamException("Order ID is empty");
    }
    else if(max > totalOrder) {
      throw new QueryParamException("Order ID Range is invalid");
    }
    resultList = myCustOrderDAO.findRange(min, max, "findOrderIDRange");

    return resultList;
  }

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public List<Custorder> ReadCustomerOrder() {

    // Check conditional get here
    return myCustOrderDAO.findAll(); 
  }

  @POST
  @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_FORM_URLENCODED})
  public Response createOrder(Custorder myCustOrder) {

    String orderID = null;

    myCustOrder.setStatus("pending");
    myCustOrder.setOrderdate(new Date());
    myCustOrder.setTotal("");

    // Persist
    myCustOrderDAO.create(myCustOrder);

    // Get Order ID

    // Embedded created URL for new customer order in response
    return Response.created(myUriInfo.getAbsolutePath().resolve(myCustOrder.getOrderid() + "/")).build();
  }

  @Path("{orderID}")
//  @Produces(MediaType.TEXT_HTML)
  public CustOrderResource ReadSingleCustomerOrder(@PathParam("orderID") String orderID) {

    int userOrderID = Integer.parseInt(orderID);
    int myOrderID = myCustOrderDAO.count();

    CustOrderResource myCustorder = null;

    if(userOrderID > myOrderID 
            || myCustOrderDAO.find(orderID) == null) {
      throw new OrderNotFoundException("Order ID Not Found");
    }

    if(!mySecurityContext.isUserInRole("admin")) {  
      // Propogates to specific resource class
      myCustorder = myResourceContext.getResource(CustOrderResource.class);
      myCustorder.setOrderID(orderID);
    }

    return myCustorder;
    //    return CustOrderResource.getInstance(myCustOrderDAO, orderID);
  }
}

子资源定位器类:
@RequestScoped
public class CustOrderResource {

  //<editor-fold defaultstate="collapsed" desc="Instance Variable">
  @Inject
  private CustOrderDAO myCustOrderDAO;

  private String orderID;

  private static final Logger myLogger = Logger.getLogger(CustOrderResource.class.getName()); 

  //</editor-fold>

  // ========================================================
  public CustOrderResource() {
  }

  private CustOrderResource(String orderID) {
    this.orderID = orderID;
  }


  public static Custorder getInstance(CustOrderDAO myCustOrderDAO, String orderID) {
    return myCustOrderDAO.find(orderID);
  }

  @GET
  @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML})
  public Custorder getCustomerOrder() {
    return myCustOrderDAO.find(orderID);
  }

  @POST
  @Consumes(MediaType.APPLICATION_XML)
  public String updateCustomerOrder() {

    return "so";

    /*try {
      myCustOrderDAO.update(myCustOrder);
    }
    catch(Exception e) {

      myLogger.log(Level.ALL, e.toString());

      throw new WebApplicationException(
              Response.status(Status.INTERNAL_SERVER_ERROR)
              .entity("Cust Order Update Failed").build());
    }*/
  }

  @DELETE
  // 415 Unsupported media type
  public String deleteCustomerOrder() {

    return "Deleted";
    //    myCustOrderDAO.delete(myCustOrder);
  }

  public String getOrderID() {
    return orderID;
  }

  public void setOrderID(String orderID) {
    this.orderID = orderID;
  }
}

我的问题是

  • 据我所知,当我们按照HTTP方法(如POST或DELETE)将资源上下文指定为参数时,资源上下文将传播到特定的资源类。如何将参数从子资源定位器方法传递到子资源类方法?

我尝试使用XML数据使用POST方法更新客户订单,但不幸的是JAX-RS运行时返回415不支持的媒体类型。

我正在使用http://code.google.com/p/rest-client/中的REST客户端来测试我的应用程序,通过将XML文件粘贴到主体选项卡内容中。这有什么问题吗?

  • JAXB是否会自动转换为XML,当我返回对象列表时?我已经测试过它返回xml格式,但只是想确认一下。返回响应对象是否更加灵活?

我想知道如何使用对象列表和URI列表或Atom XML和对象列表(Apache Abdera)构建响应对象。

  • 如何在createCustomerOrder方法中找到新持久化对象的ID?

谢谢。

请帮忙。


1
通过阅读Jersey 1.7用户指南解决了问题。谢谢。 - nicholas
做得好,peterwkc。友情提醒,你能否自己发布一个答案并接受它,这样我们就可以关闭这个问题了?此外,如果之前的问题的答案解决了你的问题,你也需要接受它们的答案。 - Zecas
1个回答

0
将对象传递到子资源定位器类中,可以使用QueryParam注释来解决。新持久化的对象是通过EntityManager工具找到的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接