6

I want to call java Api from my angular 2 app.I used typescript Map to send request in java app.
My RestEndpoint in java is like this:

@PostMapping(value = Constants.PATH_BASE + "/sync/list")
public ResponseEntity<?>configureQueueList(@RequestBody Map<String,Integer> map){
   //code here 
    }

I got this error when i try to use typescript map:

JSON parse error: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@f6b068c; line: 1, column: 1]

In postman i use this as raw body and it works

{
 "key1":"value1",
 "key2":"value2",
 .....
 .....
}

Edit 2: Type Script Endpoint

postMap(value:Map<string,number>){
    let headers = new Headers({ 'Content-Type': 'application/json' });

            let options = new RequestOptions({ headers: headers });
            return this.http.post(this.url, value, options)
            .map(success => success.status)
                .catch(this.handleError);
}
2
  • Please also provide your typescript end-point, since there seems to be the problem. Commented Jan 9, 2018 at 12:22
  • i just edit the question . Commented Jan 9, 2018 at 13:16

1 Answer 1

5

Please ensure, that your actual request body, starts with { tag, not: [. As you have written, in Postman you wrap your request with { }, whereas error message:

[JSON parse error: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token;...

...Source: java.io.PushbackInputStream@f6b068c; line: 1, column: 1]

suggests, that your actual request is wrapped with [ ] tags

EDIT (to answer your first comment): Translate your Map into Object. For example you can achieve it with this function:

function mapToObj(strMap) {
        let obj = Object.create(null);
        for (let [k,v] of strMap) {
            obj[k] = v; //look out! Key must be a string!
        }
        return obj;
    }

Then provide the result of your transformation into the http.post line

Edit 2 The above answer will work if you are using es6, if you are using es5 u can try using a dictionary like this

let map:{ [name: string]: number }={};
map["key1"]=value1;
map["key2"]=value2;
Sign up to request clarification or add additional context in comments.

2 Comments

Angular 2 converts in a json string starts with [ how i can change to start with {
I extended my answer, please take a look:)

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.