Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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?

share|improve this question
    
What you are trying to do doesn't make much sense. You can't create Java object inside (at least client side) JS. Note, that Java and JavaScript have nothing in common. What you do is to use a REST service and pass the properties required to your Java function and then create the object within the Java server code. – dirkk Aug 27 '14 at 11:13
    
also you can share the errors in console. – Jai Aug 27 '14 at 11:14
    
the question is misleading somehow. try to reconstruct. – Noypi Gilas Aug 27 '14 at 11:39
    
I edit the Q, let me know if it is clearer – soninob Aug 27 '14 at 12:50
up vote 1 down vote accepted

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.

share|improve this answer

You need to pass a javascript object in data like this : data: {param1: param1value, param2: param2value}

share|improve this answer
    
I tried it and got the above Error, did I miss something? – soninob Aug 27 '14 at 13:10

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'
share|improve this answer
    
I did try something like that as you can see in the edit Q. I missing something, how can I pass the branch descriptor as properties if is is not string? – soninob Aug 28 '14 at 5:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.