我是Dropwizard框架的新手。我正在努力创建一个类似于此处教程中提到的人员和人力资源的新资源https://github.com/dropwizard/dropwizard/tree/master/dropwizard-example。
我正在创建一个这样的文档类 -
@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调用时,会收到相当于对象的完美JSON字符串。
答案 0 :(得分:3)
要获取有关400错误的有用消息,请在球衣上注册:
environment.jersey().register(new JsonProcessingExceptionMapper(true));
它将给出有关400响应的更详细的消息,对于调试非常有用。
答案 1 :(得分:2)
一点背景:Dropwizard utilizes Jersey,泽西岛最终会给你回复400 Bad Request
回复,可能伴随着一个模糊而简洁的信息。
为了确切地看到了什么困扰杰克逊(这反过来困扰泽西岛),我开始发送一个空白(空)JSON对象,看看它是否被接受(它做了 - 以及POJO中的所有字段初始化为零)。然后我开始添加字段,发送每个这样的对象,直到我到达有问题的字段(在我的情况下,它是boolean
字段应该是Boolean
。
我想我可以在你的POJO(Document
类)中发现两个困难:
尝试将Id
的类型更改为Long
(可空长)。如果您担心在该字段中获得null
,则可以让getter返回零或任何默认值。
答案 2 :(得分:1)
我遇到了同样的问题。错误被抑制,并且在堆栈跟踪中未正确传递。 我做的是在函数周围添加一个try catch。然后在异常中添加了一个调试器点。我弄清楚了确切的原因。
你可以尝试这样的事情。
@POST
@UnitOfWork
public Document createDocument(Document document) throws Exception{
....
}
在Exception类中添加调试器点。您将找到解析失败的确切原因。
希望我很清楚,这有帮助!
答案 3 :(得分:0)
Http状态400表示&#34;错误请求&#34;。它是什么,你发送的json不是有效的Document
。
这反过来意味着你永远不会到达
@POST
@UnitOfWork
public Document createDocument(Document document){}
要解决它,请尝试传递json:
String input = "{\"id\":\"123456789\",\"processed\":\"new process\",\"ProcessingSetID\":\"new iD\"}";
将123456789
替换为您的实际ID。
PS。为Document
创建DTO而不是传递实际实体可能是一个好主意(取决于您的方案)。
答案 4 :(得分:0)
如果您在run(...)
方法中的Dropwizard * Application.java 中注册泽西岛的CsrfProtectionFilter
,请确保添加X-Requested-By
标题所有状态更改HTTP调用(POST
,PUT
等)。如果在请求中找不到该标头,服务器将返回 HTTP 400错误请求。