Dropwizard example gives 400 errors when creating a new resource

I am new to the Dropwizard framework. I am trying to work on creating a new resource similar to the person and people resource mentioned in the tutorial here https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example .

I create such a document class -

@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 Dao document is as follows:

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 a 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 get a 400 response from my client request, please find the client request below

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());
}

, POST . , JSON, . , GET, JSON, .

+4
4

: Dropwizard , - , 400 Bad Request, , .

, (, , ), , () JSON- , ( - POJO, ). , , ( boolean, boolean).

, POJO ( Document):

+2

. . , , . ​​ . .

- .

@POST
@UnitOfWork
public Document createDocument(Document document) throws Exception{
....
}

Exception. .

, , !

+1

400 Http " ". , json, , Document. , , ,

@POST
@UnitOfWork
public Document createDocument(Document document){}

, json:

String input = "{\"id\":\"123456789\",\"processed\":\"new process\",\"ProcessingSetID\":\"new iD\"}";

123456789 .

PS. ( ) DTO Document .

0
source

If you register the Jersey CsrfProtectionFilterin your Dropwizard * Application.java as part of the method run(...), be sure to re-add the header X-Requested-Byto all of your changes to the HTTP calls that you modify ( POST, PUTetc.). The server will return an HTTP 400 Bad Request if this header is not found in the request.

0
source

Source: https://habr.com/ru/post/1538170/


All Articles