Java 是否将数据发布到dropwizard资源?
我正在尝试确定如何将数据发布到dropwizard资源 例如,假设我拥有以下资源:Java 是否将数据发布到dropwizard资源?,java,rest,dropwizard,Java,Rest,Dropwizard,我正在尝试确定如何将数据发布到dropwizard资源 例如,假设我拥有以下资源: @GET @Timed @Path("/update/{id}") public int updateRecord( @PathParam("id") int id) throws JsonParseException, JsonMappingException, IOException // Logic here I guess?! // Returning 0 to kill the error r
@GET
@Timed
@Path("/update/{id}")
public int updateRecord( @PathParam("id") int id) throws JsonParseException, JsonMappingException, IOException
// Logic here I guess?!
// Returning 0 to kill the error regarding not returning antyhing in my IDE.
return 0;
}
现在我希望能够发表jquery文章,例如:
$.post(“,jsonUpdate字符串)
因为记录“jsonUpdateString”是一个字符串化的JSON数组——我已经知道如何将其映射到java数据结构,就像我以前硬编码过的那样——我只需要知道我需要向我的资源中添加什么才能在我的java端实际使用“jsonUpdateString”。类似的东西应该可以工作:
static class Entity {
@JsonProperty String name;
}
@POST
@Timed
@Path("update/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public int updateRecord(@PathParam("id") int id, List<Entity> entities) {
// Do something with entities...
return 0;
}
有几件事:
- 如果您的方法有一个未注参数(
,在本例中),Jersey将尝试将请求实体映射到此对象entities
- 让Jersey/Jackson为您进行JSON转换(请参见
)Entity
- 确保JQuery客户端正在设置请求头
内容类型:application/json
curl -H "Content-Type: application/json" --data '[{"name":"foo"}]' http://localhost:8080/update/1