Skip to content

@JacksonXmlRootElement malfunction when using it with XmlMapper in multi-thread environment #171

@wanglingsong

Description

@wanglingsong

This is the sample to reproduce the bug. The "localName" override is failed in some of thread, printing original class name "TestModel" instead of "model". XmlMapper seems not thread safe.

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

@JacksonXmlRootElement(localName = "model")
public class TestModel {


    public static void main(String[] s) throws Exception {

        final ObjectMapper xmlMapper = new XmlMapper();

        for (int i=0;i < 100;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(xmlMapper.writeValueAsString(new TestModel()));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        Thread.sleep(10000L);

    }

}

Output:

<model/>
<model/>
<model/>
<model/>
<model/>
<model/>
<model/>
<TestModel/>
<model/>
<model/>
<model/>
<model/>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions