1

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?

4
  • 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. Commented Aug 27, 2014 at 11:13
  • also you can share the errors in console. Commented Aug 27, 2014 at 11:14
  • the question is misleading somehow. try to reconstruct. Commented Aug 27, 2014 at 11:39
  • I edit the Q, let me know if it is clearer Commented Aug 27, 2014 at 12:50

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I tried it and got the above Error, did I miss something?
0

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'

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.