Dropwizard example giving 400 error when creating new resource

dropwizard, java, json, rest

Solution

To get helpful message regarding 400 error, register this on jersey:

environment.jersey().register(new JsonProcessingExceptionMapper(true));

It will give more detailed message on 400 response, useful for debugging.

Problem

I am new to the Dropwizard framework. I am trying to work on creating a new resource similar to person and people resource mentioned in the tutorial here https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example. I am creating a document class like this - ``` @Entity @Table(name = "document") @NamedQueries({ @NamedQuery( name = "com.example.helloworld.core.Document.findAll", query = "SELECT d FROM Document d" ), @NamedQuery( name = "com.example.helloworld.core.Document.findById", query = "SELECT d FROM Document d WHERE d.Id = :Id" ) }) public class Document { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long Id; @Column(name = "ProcessingSetID") private String ProcessingSetID; @Column(name = "processed") private String processed; public long getId() { return Id; } public void setId(long id) { this.Id = id; } public String getProcessingSetID() { return ProcessingSetID; } public void setProcessingSetID(String processingSetID) { ProcessingSetID = processingSetID; } public String getProcessed() { return processed; } public void setProcessed(String processed) { this.processed = processed; } } ``` My document Dao is like this, ``` public Optional<Document> findById(Long id) { return Optional.fromNullable(get(id)); } public Document create(Document document) { return persist(document); } public List<Document> findAll() { return list(namedQuery("com.example.helloworld.core.Document.findAll")); } } ``` I am trying to call the POST method on my document resource, ``` @Path("/documents") @Produces(MediaType.APPLICATION_JSON) public class DocumentsResource { private final DocumentDao documentDAO; private static final Logger log = LoggerFactory.getLogger(DocumentsResource.class); public DocumentsResource(DocumentDao documentDAO) { this.documentDAO = documentDAO; } @POST @UnitOfWork public Document createDocument(Document document) { log.info("inside POST method of document."); System.out.println("inside POST method of document....."); return documentDAO.create(document); } @GET @UnitOfWork public List<Document> listDocuments() { return documentDAO.findAll(); } } ``` But I am getting a 400 response back from my client request, please find below the client request ``` Client client = Client.create(); WebResource webResource = client.resource("http://localhost:8080/documents"); String input = "{\"processed\":\"new process\",\"ProcessingSetID\":\"new iD\"}"; ClientResponse response = webResource.type("application/json").post(ClientResponse.class, input); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } ``` I tried to debug the problem, but the call is not reaching the POST method at the first place. It seems that it is not creating the document object from the JSON string, but i could not see a reason for that. Also when I do an entry directly in my database and make a GET call, perfect JSON string equivalent to object is received.

Original source