users@jersey.java.net

[Jersey] Rest Post Unsupported Media Type

From: peterwkc <peterapiit_at_gmail.com>
Date: Tue, 21 Jun 2011 02:45:55 -0700 (PDT)

Hello to all, i have developed a simple customer order rest web service and
everything is works fine besides the post method to create a new order,
delete order and update order.


1. I using rest client from http://code.google.com/p/rest-client/ to test my
application where i set the content type to xml and posted this data in body
but return HTTP/1.1 400 Bad Request. What wrong with it ?

<?xml version="1.0" encoding="UTF-8"?>
   
       
           
               No:26, JLN Kp 2/3, Tmn Kota Perdana, Bandar Putra Permai,
Seri kembangan, 43300, Selangor
               0149221394
               peterwkc
               c01
           
           pos
           2011-06-15T00:00:00+08:00
           3
           cash
           
               80.00
               p03
               book3
           
           pending
           80
       
    


Create Order :

@POST
  @Consumes({MediaType.WILDCARD, MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML,
    MediaType.APPLICATION_FORM_URLENCODED})
  public Response createOrder(Custorder myCustOrder, @Context UriInfo
myUriInfo) {
    
    String orderID = null;
    CacheControl myCache = new CacheControl();
    
    myCustOrder.setStatus("pending");
    myCustOrder.setOrderdate(new Date());
    myCustOrder.setTotal("");
    
    // Persist
    myCustOrderDAO.create(myCustOrder);

    // Get Order ID
    
    // ======================================================
    myCache.setNoCache(false);
    myCache.setMaxAge(MAX_AGE);
    myCache.setMustRevalidate(true);
    myCache.setNoStore(false);
    myCache.setProxyRevalidate(true);
    
    // Embedded created URL for new customer order in response
    return Response.created(
            myUriInfo.getAbsolutePath()
              .resolve(myCustOrder.getOrderid() + "/"))
            .cacheControl(myCache).entity(myCustOrder).build();
  }


2. How to pass the data from root resource class to sub resource method ?

// Root resource class
@Path("{orderID}")
  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;
  }

// Sub resource locator method
@POST
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
    MediaType.APPLICATION_ATOM_XML})
  public String updateCustomerOrder(Custorder myCustorder) {
   return "Hello";
}

Please help.

Thanks.



--
View this message in context: http://jersey.576304.n2.nabble.com/Rest-Post-Unsupported-Media-Type-tp6499271p6499271.html
Sent from the Jersey mailing list archive at Nabble.com.