如何在angular JS中发送java对象

如何在angular JS中发送java对象,java,javascript,angularjs,http,post,Java,Javascript,Angularjs,Http,Post,我有一个JS文件,它试图执行POST方法,但需要一个特定的java对象作为输入 以下是服务器中Post方法的签名: @Path("/branches") public class BranchResource { @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public Response post(BranchDescriptor branch) { . . 下面是JS函数

我有一个JS文件,它试图执行POST方法,但需要一个特定的java对象作为输入

以下是服务器中Post方法的签名:

@Path("/branches")
public class BranchResource {

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response post(BranchDescriptor branch) {
.
.
下面是JS函数的示例(使用Angular)

我收到了以下错误:

??? 27, 2014 3:27:48 PM com.sun.jersey.spi.container.ContainerRequest getEntity
SEVERE: A message body reader for Java class    xxx.api.BranchDescriptor, and Java type class xxx.BranchDescriptor, and MIME media type application/octet-stream was not found.
The registered message body readers compatible with the MIME media type are:
application/octet-stream ->
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.RenderedImageProvider
*/* ->
因此,我尝试添加这样的数据:

$http({
   url : "rest/branches",
   method : "POST",
   data : {branch : "way to get Branch descriptor" },
   headers : {
        "Content-Type" : "application/json; charset=utf-8",
        "Accept" : "application/json"
   }
})
数据:{ “_protectedBranch”:“pf_某物”, “_newBranch”:“某个_分支”, “_commitd”:“some_commitd”, “提交人”:“某人” },

并得到以下错误:

??? 27, 2014 3:42:46 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The exception contained within MappableContainerException could not be mapped to a response, re-throwing to the HTTP container
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "_protectedBranch" (Class xxx.BranchDescriptor), not marked as ignorable
 at [Source: HttpInputOverHTTP@66aee27d; line: 1, column: 22] (through reference chain: xxx.BranchDescriptor["_protectedBranch"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
如何传递BranchDescriptor对象以进行发送?
我缺少什么?

您不需要
数据类型,您可以尝试发送一个对象,而不是像这样的字符串:

$http({
   url : "rest/branches",
   method : "POST",
   data : {branch : "way to get Branch descriptor" },
   headers : {
        "Content-Type" : "application/json; charset=utf-8",
        "Accept" : "application/json"
   }
})

在文件中:

$http服务将自动向所有请求添加某些http头

  • 接受:application/json,text/plain,*/*
  • 内容类型:application/json

Angular js使用上述头发送请求,而jQuery则发送以下消息:

  • '应用程序/x-www-form-urlencoded;字符集=UTF-8'

您需要在
data
中传递一个javascript对象,如下所示:
data:{param1:param1value,param2:param2 value}

好的,找出我缺少的东西,这只是创建分支并将其发送到http中的数据的一种方法:

var branch = {
            protectedBranch:"some_branch",
            newBranch:"some_branch",
            commitId: "some_commit",
            commiter:"some_commiter"
    }
    $scope.retry = function() {
        $http({
               url : "rest/branches",
               method : "POST",
               data : branch,
               headers : {
                     "Content-Type" : "application/json; charset=utf-8",
                     "Accept" : "application/json"
               }
        }).success(function(data) {
               $scope.items = data;
        }).error(function(data) {
               alert('err');
        });

希望它能对其他人有所帮助。

你想做的事情没有多大意义。您不能在JS内部(至少在客户端)创建Java对象。请注意,Java和JavaScript没有任何共同之处。您要做的是使用REST服务并将所需的属性传递给Java函数,然后在Java服务器代码中创建对象。此外,您还可以在控制台中共享错误。这个问题在某种程度上具有误导性。尝试重建。我编辑了Q,让我知道它是否清晰。我尝试了它并得到了上面的错误,我错过了什么吗?我确实尝试了类似的事情,正如你在编辑Q中看到的那样。我错过了什么,如果不是字符串,我如何将分支描述符作为属性传递?