gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:44 26 4
gpt4 key购买 nike

我有一个试图执行 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com