Reputation: 456
I have JS file that is trying to do a POST method, but needs a specific java object as input.
here is the signature of the Post method in the server:
@Path("/branches")
public class BranchResource {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response post(BranchDescriptor branch) {
.
.
here is the the JS function (using 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');
});
};
I was receiving the following errors:
??? 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
*/* ->
So, I tried to add a data like that:
data : { "_protectedBranch" : "pf_something", "_newBranch" : "some_branch", "_commitId" : "some_commit", "_commiter" : "someone" },
and got the following error:
??? 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)
How can I Pass the BranchDescriptor object for sending? What am I missing?
Upvotes: 1
Views: 4887
Reputation: 456
OK, found out what I was missing, it was just a way of creating the branch and send it to the data in the 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');
});
hope it will be help to others.
Upvotes: 1
Reputation: 74738
You don't need the dataType
and you can try sending an object instead of string like that:
$http({
url : "rest/branches",
method : "POST",
data : {branch : "way to get Branch descriptor" },
headers : {
"Content-Type" : "application/json; charset=utf-8",
"Accept" : "application/json"
}
})
In the docs:
The $http service will automatically add certain HTTP headers to all requests.
- Accept: application/json, text/plain, * / *
- Content-Type: application/json
Angular js sends the request with the above headers unlike jQuery which sends this:
- 'application/x-www-form-urlencoded; charset=UTF-8'
Upvotes: 0
Reputation: 5329
You need to pass a javascript object in data
like this : data: {param1: param1value, param2: param2value}
Upvotes: 0