【发布时间】:2014-08-27 11:02:52
【问题描述】:
我有一个 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)
$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
*/* ->
所以,我尝试添加这样的数据:
数据:{ "_protectedBranch" : "pf_something", "_newBranch" : "some_branch", "_commitId" : "some_commit", “_commiter”:“某人” },
并得到以下错误:
??? 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 对象进行发送? 我错过了什么?
【问题讨论】:
-
您尝试做的事情没有多大意义。您不能在(至少是客户端)JS 中创建 Java 对象。请注意,Java 和 JavaScript 没有任何共同之处。您所做的是使用 REST 服务并将所需的属性传递给 Java 函数,然后在 Java 服务器代码中创建对象。
-
你也可以在控制台分享错误。
-
这个问题在某种程度上具有误导性。尝试重建。
-
我编辑了Q,如果更清楚请告诉我
标签: java javascript angularjs http post