java - Response entity and response in client differ -
i've got spring rest web application. i've got method returns response entity.
@requestmapping(value = "/shoes", method = requestmethod.get, produces = mediatype.application_json_value) public responseentity<?> getshoes() { collection<shoes> shoes = shoesservice.findall(); responseentity responseentity = new responseentity<>(shoes, httpstatus.ok); return responseentity; }
when set breakpoint on last line, can see responseentity
contains list of following objects:
shoes{id=1, localization=localization{id=1, city='denver'}, category=category{id=1, name='wellingtons', group='male'}, size=9}
but when send request in client app, json, contains id , size:
{ "id": 1, "size": 9 }
i wonder why don't receive localization , category.
here shoes
class:
@table(name = "shoes") public class shoes{ @column(name = "id") @id @generatedvalue(strategy = generationtype.identity) private long id; @jsonbackreference @manytoone private localization localization; @jsonbackreference @manytoone private category category; @column(name = "size") private int size; ... }
if have bi-directional associations, have declare object has role of parent (annotated jsonmanagedreference
) , has role of child (annotated jsonbackreference
) break cycles.
you annotated both properties (localization
, category
) not serialized, see jsonbackreference
:
annotation used indicate associated property part of two-way linkage between fields; , role "child" (or "back") link. value type of property must bean: can not collection, map, array or enumeration. linkage handled such property annotated annotation not serialized; , during deserialization, value set instance has "managed" (forward) link.
Comments
Post a Comment