简体   繁体   English

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

[英]RESTful Web Service - resource context and response questions

I have developed a simple RESTful web service. 我开发了一个简单的RESTful Web服务。

Root Resource Class: 根资源类:

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

Sub Resource Locator Class : 子资源定位器类:

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

My question is 我的问题是

  • AFAIK, the resource context will propagate to specific resource class when we specify it as an argument according to the HTTP method like POST or DELETE. AFAIK,当我们根据HTTP或DELETE等HTTP方法将其指定为参数时,资源上下文将传播到特定的资源类。 How do I pass the parameter from sub resource locator method into sub resource class method? 如何将参数从子资源定位器方法传递到子资源类方法?

I tried to update customer order using post method with XML data but unfortunately the JAX-RS runtime returns 415 Unsupported media type. 我尝试使用带有XML数据的post方法更新客户订单,但不幸的是JAX-RS运行时返回415不支持的媒体类型。

I am using the REST client from http://code.google.com/p/rest-client/ to test my application, by pasting an XML file into the body tab content. 我正在使用http://code.google.com/p/rest-client/中的REST客户端来测试我的应用程序,方法是将XML文件粘贴到正文选项卡内容中。 What is wrong with it? 这有什么问题?

  • Does the JAXB automatically convert to XML when I return a list of objects? 当我返回对象列表时,JAXB是否自动转换为XML? I have tested and it return xml format but just want confirmation. 我已经测试过,它返回xml格式,但只是想要确认。 Is it more flexible to return response object? 返回响应对象更灵活吗?

I wonder how to build a response object with list of object and list of URI or Atom XML with list of object (Apache Abdera). 我想知道如何使用对象列表和对象列表(Apache Abdera)的URI或Atom XML列表构建一个响应对象。

  • How to find out a id of a newly persisted object into database in my createCustomerOrder method ? 如何在我的createCustomerOrder方法中找到新持久化对象的id到数据库中?

Thanks. 谢谢。

Please help. 请帮忙。

Passing object into sub resource locator class is solve by using QueryParam annotation. 通过使用QueryParam注释来解决将对象传递到子资源定位器类的问题。 Newly persisted object is finding using EntityManager util. 新持久化的对象是使用EntityManager util查找的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM