java - Expected an array of objects but got an object in an object -


in project i'm working on got angular exception: error: [$resource:badcfg] error in resource configuration. expected response contain array got object?

in search find solution entered url of web service directly browser , surprisingly did not receive array expected.

the web service class:

@path("/menu") public class menuhandler {     @get     @path("/cls")     @produces(mediatype.application_json)     public list<clazz> getclss()    {         clazz clazz = new clazz();         clazz.setfoo("foo");         clazz.setbar("bar");          arraylist<clazz> clazzes = new arraylist<>();         clazzes.add(clazz);          return clazzes;     } } 

when enter url http://localhost:8080/myproject/rest/menu/cls expect see json array json objects:

[ {"foo": "foo", "bar": "bar"} ] 

but instead, receive json object property json object expecting without array:

{   "clazz": {     "foo": "foo",     "bar": "bar"   } } 

so wondered why there no array , happen when add clazz object. in case still json object time 1 of parameters json array expect have start.

{   "clazz": [     {       "foo": "foo",       "bar": "bar"     },     {       "foo": "foo2",       "bar": "bar2"     }   ] } 

can explain me why behavior happening , thinking went wrong?

so spotted problem. able reproduce it. seems forgot enable pojomappingfeature

<init-param>     <param-name>com.sun.jersey.api.json.pojomappingfeature</param-name>     <param-value>true</param-value> </init-param> 

this uses jackson deserialize/serialize. if not set, uses jersey's internal json provider(s) deserialize/serialize. not sure sure how configure default providers, think it's safer go jackson anyway.


aside, in comments above saying test if jackson being used. 1 way accomplish create contextresolver , and s.o.p. inside getcontext method. method called when mapper needed. if jackson being used, s.o.p should print

@provider public class objectmappercontextresolver implements contextresolver<objectmapper> {      private final objectmapper defaultmapper;      public objectmappercontextresolver() {         defaultmapper = new objectmapper();     }      @override     public objectmapper getcontext(class<?> type) {         system.out.println("--- getcontext ---");         return defaultmapper;     }   } 

note way make configurations jackson serization/deserialization (just configure mapper).


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -