Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否将数据发布到dropwizard资源?_Java_Rest_Dropwizard - Fatal编程技术网

Java 是否将数据发布到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

我正在尝试确定如何将数据发布到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 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;
}
有几件事:

  • 如果您的方法有一个未注参数(
    entities
    ,在本例中),Jersey将尝试将请求实体映射到此对象

  • 让Jersey/Jackson为您进行JSON转换(请参见
    Entity

  • 确保JQuery客户端正在设置请求头
    内容类型:application/json


谢谢-但当我这样做时,我会得到一个错误:“405”有什么想法我可以修复吗?你确定你有正确的路径吗?405是“不允许使用方法”-因此它听起来像是向服务器发送了错误类型的请求。
curl -H "Content-Type: application/json" --data '[{"name":"foo"}]' http://localhost:8080/update/1