com.fasterxml.jackson.databind.JsonMappingException:无法实例化类型的值[simple type,class car.Part]

时间:2016-05-11 17:24:52

标签: java json jackson jackson-modules jackson-dataformat-xml

我正在尝试将此XML反序列化为Parts对象:

<Parts>
    <Part>
                <Name>gearbox</Name>
                <Year>1990</Year>
    </Part>
    <Part>
                <Name>wheel</Name>
                <Year>2000</Year>
    </Part>
</Parts>

Car.java

package problem.car;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Car {

    public static void main(String args[]) {
        try {
            String xml = "<Parts>\n"
                    + " <Part>\n"
                    + "             <Name>gearbox</Name>\n"
                    + "             <Year>1990</Year>\n"
                    + " </Part>\n"
                    + " <Part>\n"
                    + "             <Name>wheel</Name>\n"
                    + "             <Year>2000</Year>\n"
                    + " </Part>\n"
                    + "</Parts>";
            Parts parts = (Parts) deserialize(Parts.class, xml);
        } catch (IOException ex) {
            Logger.getLogger(Car.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static final Object deserialize(final Class clazz, final String xml) throws IOException {
        ObjectMapper xmlMapper = new XmlMapper();
        xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz"));
        Object object;
        try {
            object = xmlMapper.readValue(xml, clazz);
        } catch (com.fasterxml.jackson.databind.exc.InvalidFormatException ex) {
            xmlMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            object = xmlMapper.readValue(xml, clazz);
        }
        return object;
    }
}

Parts.java

package problem.car;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.ArrayList;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "Part"
})
public class Parts {

    @JsonProperty("Part")
    private List<Part> Part = new ArrayList<>();

    @JsonProperty("Part")
    public List<Part> getPart() {
        return Part;
    }

    @JsonProperty("Part")
    public void setPart(List<Part> Part) {
        this.Part = Part;
    }

}

Part.java

package problem.car;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "Name",
    "Year"
})
public class Part {

    @JsonProperty("Name")
    private String Name;
    @JsonProperty("Year")
    private String Year;

    @JsonProperty("Name")
    public String getName() {
        return Name;
    }

    @JsonProperty("Name")
    public void setName(String Name) {
        this.Name = Name;
    }

    @JsonProperty("Year")
    public String getYear() {
        return Year;
    }

    @JsonProperty("Year")
    public void setYear(String Year) {
        this.Year = Year;
    }

}

我的代码没有看到任何问题,但为什么它一直给我以下内容?

com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class problem.car.Part] from String value ('gearbox'); no single-String constructor/factory method
 at [Source: java.io.StringReader@598067a5; line: 3, column: 28] (through reference chain: problem.car.Parts["Part"]->java.util.ArrayList[0])

1 个答案:

答案 0 :(得分:0)

您需要禁用&#34; wrapped&#34;列表,以匹配结构。 这应该在xml dataformat github项目页面的README页面上解释。