我是Dropwizard框架的新手。我正在尝试创建一个类似于https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example教程中提到的person和people资源的新资源。
我正在创建这样的文档类-
@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;
}
}
我的文件Dao就是这样,
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"));
}
}
我正在尝试在我的文档资源上调用POST方法,
@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();
}
}
但是我从客户请求中得到了400条回复,请在客户请求下方找到
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调用时,也会收到与object等效的完美JSON字符串。
最佳答案
要获得有关400错误的有用消息,请在jersey上注册:
environment.jersey().register(new JsonProcessingExceptionMapper(true));
它将给出有关400响应的更详细的消息,对调试很有用。
关于java - Dropwizard示例在创建新资源时给出400错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23307933/