JSON Serialization loop (infinite recursion) in Wildfly

南楼画角 提交于 2019-12-01 23:54:26

The solution is to create a class which implement MessageBodyWriter<Object> using Jackson's ObjectMapper:

@Provider
@Produces("application/json")
public class JacksonMapper implements MessageBodyWriter<Object> {

    @Override
    public boolean isWriteable(Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public long getSize(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType) {
        return 0;
    }

    @Override
    public void writeTo(Object object, Class<?> aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap, OutputStream outputStream)
            throws IOException, WebApplicationException {
        outputStream.write(new ObjectMapper().writeValueAsBytes(object));
    }

}

There's no need for any exclusion or Wildfly-specific descriptors.

It doesn't matter if you include the dependencies or not (compile or provided scopes both work fine) as Jackson 2 is included in Wildfly. However, for an unknown reason, it is near to impossible to deactivate Jackson 1.

This solution brings Jackson 2 into the works. Now you can easily avoid serialization loops using the @JsonIdentityInfo annotation. More info here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!