I developed a simple RESTful web service.
Root resource class:
@Path("/order")
@RequestScoped
public class CustOrderContainerResource {
@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() {
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("");
myCustOrderDAO.create(myCustOrder);
return Response.created(myUriInfo.getAbsolutePath().resolve(myCustOrder.getOrderid() + "/")).build();
}
@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")) {
myCustorder = myResourceContext.getResource(CustOrderResource.class);
myCustorder.setOrderID(orderID);
}
return myCustorder;
}
}
Resource Locator Class:
@RequestScoped
public class CustOrderResource {
@Inject
private CustOrderDAO myCustOrderDAO;
private String orderID;
private static final Logger myLogger = Logger.getLogger(CustOrderResource.class.getName());
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";
}
@DELETE
public String deleteCustomerOrder() {
return "Deleted";
}
public String getOrderID() {
return orderID;
}
public void setOrderID(String orderID) {
this.orderID = orderID;
}
}
My question
- AFAIK, the resource context will apply to a specific resource class when we specify it as an argument according to the HTTP method, such as POST or DELETE. How to pass a parameter from an auxiliary resource locator method to an auxiliary resource class method?
I tried updating the sales order using the post method with XML data, but unfortunately the JAX-RS runtime returns 415 Unsupported media type.
I am using the REST client with http://code.google.com/p/rest-client/ to test my application by inserting an XML file into the contents of the abalone. What's wrong with it?
, URI Atom XML (Apache Abdera).
.
, .