1

Ok, so I am building a RESTful service in Java using Jersey.

I have implemented the POST which works wonderfully, which creates a new row in the Category table in my Database. Now when I try to do the DELETE, I pass it the same row in JSON, but it returns a "400: Bad Request - the request cannot be fulfilled due to bad syntax".

I am confused, since the JSON is copied and pasted from the result I get when I do a GET on that specific category.

Here is the relevant code:

@Path("/categories/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class CategoryResource {

@POST
public Response addCategory(Category category, @Context UriInfo uriInfo)
{
    JSONArray json_array = category_service.addCategory(category);
    URI uri = uriInfo.getAbsolutePathBuilder().path(category.getCategory_name()).build();
    return Response.created(uri)
            .status(Status.CREATED)
            .entity(json_array.toString())
            .build();
}

@DELETE
public Response deleteCategory(Category category)
{
    System.out.println("Category = " + category.toString());
    category_service.deleteCategory(category);
    return Response.ok().build();
}

The system.out.println() is never executed because it seems like it fails to do the encoding of the category. The Category class is here, replete with the usual getters and setters:

@XmlRootElement
public class Category {

private int category_id;
private String category_name;
private boolean child;
private int parent_id;
private boolean category_state;
private String category_reason;

public Category() {

}

The JSON that is being passed in the raw looks like this:

{
  "category_id": 1,
  "category_name": "tennis",
  "child": true,
  "parent_id": 4,
  "category_state": true,
  "category_reason": "I Like Tennis"
}

Note that this has been copied and pasted from the result of the GET....

Tom O'Brien
  • 1,741
  • 9
  • 45
  • 73
  • 1
    Some servers (for example Grizzly) can't (or needs to be configured to) handle DELETE with a body. Generally you don't need a body with DELETE. In many cases, the id from the URI should be enough find the resource to delete. – Paul Samsotha Jul 03 '15 at 10:51
  • Why do you send body for delete action? – Opal Jul 03 '15 at 10:53
  • No real reason - had just implemented the POST with a body, and just assumed a DELETE wouldn't complain. Is there much configuration required to allow this, or is it considered a no-no... and why? – Tom O'Brien Jul 03 '15 at 10:57
  • You can see the [semantics on DELETE](https://tools.ietf.org/html/rfc7231#section-4.3.5). Near the last paragraph of the section, it says that there are no defined semantics for a payload in the request, just that some servers might reject it. This was the case originally with Grizzly, but after some requests, they made it configurable. From a practical perspective, the only part of your body that is really required to delete the resource is the id. Even in a ORM framework, just have a complete entity is not enough to delete it, as the object is not managed. You still need to look it up. – Paul Samsotha Jul 03 '15 at 11:10
  • Generally the is enough to look it up. And in most cases, the id is included in the URI, or the URI identifier is enough to look it up. So you should change the method to be annotated like you would GET for a single resource. `@Path("{id}")`. Not sure which server you are using, but just for informational purposes, if you are using Grizzly, you can see the bottom of [this answer](http://stackoverflow.com/a/29639878/2587435) for how to configure. Any other server, I am not sure. – Paul Samsotha Jul 03 '15 at 11:12
  • I use Glassfish - I have got it working with the identifier, was just wondering why it wouldn't work. Thanks for that.... – Tom O'Brien Jul 03 '15 at 11:38

1 Answers1

0

So as was made clear in the comments by peeskillet, with a DELETE you don't give any body.

Tom O'Brien
  • 1,741
  • 9
  • 45
  • 73