I have the following method, that receives XML and creates a new book in the database:
@PUT
@Path("/{isbn}")
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
public SuccessfulRequestMessage createBook(JAXBElement<Book> bookParam,
@PathParam("isbn") String isbn) {
if(bookParam == null)
{
ErrorMessage errorMessage = new ErrorMessage(
"400 Bad request",
"To create a new book you must provide the corresponding XML code!");
throw new MyWebServiceException(Response.Status.BAD_REQUEST,
errorMessage);
}
....................................................................
}
The problem is that when I don’t send anything in the message body, the exception is not thrown. How could I check if the message body is empty?
Thanks!
Sorin
I found out about a little trick that can be done: instead of sending MediaType.APPLICATION_XML, I send application/x-www-form-urlencoded, represented by only one parameter, and this parameter will contain the XML code. Then I can check if the parameter is null or empty. Then, from the content of the parameter, I construct a JAXBElement.
The code is the following: