Spring MVC with Jackson 2.2: “HttpMediaTypeNotAcceptableException: Could not find acceptable representation”

后端 未结 1 798
Happy的楠姐
Happy的楠姐 2021-01-06 12:40

So I\'m struggling to get my beans encoded in json... First I tried with codehaus\' jackson 1.9.13 and Spring MVC 3.1.1. I had some issues until I managed to get to \"Direct

1条回答
  •  一向
    一向 (楼主)
    2021-01-06 13:10

    Alright, I figured this out.

    First, as suspected, Spring MVC wasn't calling Jackson to serialize the entities. As I found here, I needed to modify mvc:annotation-driven to

    
        
            
        
    
    

    After that was done, I was back to Direct self-reference exception... So after I looked that up for a good hour, I finally found I 'simply' needed to add an annotation to some classes.

    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
    

    This annotation means that if some object is repeated while in the serializer it will be serialized once then after that will only be referenced by an @id created by the serializer.

    0 讨论(0)
提交回复
热议问题