java - 如何在 Angular JS中发送java对象

标签 java javascript angularjs http post

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

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

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

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response post(BranchDescriptor branch) {
.
.

这是 JS 函数(使用 Angular)

$scope.retry = function() {
    $http({
           url : "rest/branches",
           method : "POST",
           dataType : "json",//not sure is needed
           data : "way to get Branch descriptor "
           headers : {
                 "Content-Type" : "application/json; charset=utf-8",
                 "Accept" : "application/json"
           }
    }).success(function(data) {
           $scope.items = data;
    }).error(function(data) {
           alert('err');
    });
 };

我收到以下错误:

??? 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
*/* ->

所以,我尝试添加这样的数据:

data : { "_protectedBranch" : "pf_something", "_newBranch" : "some_branch", "_commitId" : "some_commit", "_commiter" : "someone" },

出现以下错误:

??? 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: 中的数据的方法:

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');
        });

希望对其他人有所帮助。

关于java - 如何在 Angular JS中发送java对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25525477/

相关文章:

javascript - AngularJS中如何通过templateUrl传递URL参数?

javascript - 使用angularjs读取Json数据

java - 程序的大 O 表示法(最坏情况)

java - GWT + 对象持久化 + 维护内存中的数据

java - 如何在 AES-128、AES-192 和 AES-256 之间切换

java - 比较整数时出现意外结果

javascript - 获取在 JavaScript 中调用函数调用者的 "this"

javascript - 我如何在 Rx Observable 上设置 `await`?

javascript - JQuery:对所有按钮仅使用一个监听器有什么缺点吗?

javascript - 如何在 Javascript 中检查所选时间是否在给定时间范围内