Jax-RS response comes in HTML/text instead of JSON

I have a front-end react application, where i make a request to a REST Jax-RS backend server.

Here is the request being sent

  deletePost = (post) =>{
return deleter(config.restUrl + `posts/${post}`)
 }

Here i get the standart URL for my backend, with a 'deleter' function, which is just a standardized fetch delete method (which has also worked with other entities as well).

Here is my Jax-RS resource:

@DELETE
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@Path("/{id: [0-9]+}")
public Response deletePost(@HeaderParam("authorization") String token, @PathParam("id") Integer id) throws ResourceNotFoundException, AuthenticationException
{
    AuthenticationContext authenticationContext = authenticationFacade.authenticateBearerHeader(token);
    Post                  post                  = postFacade.delete(authenticationContext, id);
    return Response.ok(gson.toJson(PostDTO.basic(post))).build();
}

The problem is that it gives me an error saying that the form is HTML/text:

MessageBodyWriter not found for media type\u003dtext/html, type\u003dclass com.group3.sem3exam.rest.dto.PostDTO, genericType\u003dclass com.group3.sem3exam.rest.dto.PostDTO

Since it's implying that it is the PostDTO that has the error, I went to check the basic method, which converts the entity into a Data Transfer Object, to be posted back to the client side.

   public static PostDTO basic(Post post)
{
    return new PostDTO(
            post.getId(),
            post.getContents(),
            post.getCreatedAt(),
            null,
            ImageDTO.list(post.getImages(), ImageDTO::withoutUser)
    );
    }

Here it just calls the method which returns a new instance of the object.

I have not seen this error before, and I'm not sure how to handle it?

Upvotes: 4

Views: 1131

Answers (1)

Try

return Response.status(Status.OK).entity(new Gson().toJson(PostDTO.basic(post))).build();

Upvotes: 2

Related Questions