【问题标题】:JAXB:: Unmarshalling SOAP response and getting null in nested objectsJAXB:: 解组 SOAP 响应并在嵌套对象中获取 null
【发布时间】:2019-08-28 18:03:13
【问题描述】:

调用 SOAP 服务后,我可以得到如下所示的 XML 响应:

<DecodedVinResult VIN="3FA6P0HD4KR236463">
    <VINValidationResult checkDigit="TRUE" decodeFlag="true" invalidCharacters="false" invalidVIN="false" unknownVINPattern="false" unsupportedModelYear="false" validFirstPosition="true"></VINValidationResult>
    <AssemblyPlant>
        <AttributeCode>0</AttributeCode>
        <AttributeValue>HERMOSILLO, </AttributeValue>
    </AssemblyPlant>
    <BrakesPower>
        <AttributeCode>0</AttributeCode>
        <AttributeValue></AttributeValue>
    </BrakesPower>
    <BrakesWeightRatingGVWR>
        <AttributeCode>3568</AttributeCode>
        <AttributeValue></AttributeValue>
    </BrakesWeightRatingGVWR>
    <RestraintsDriver>
        <AttributeCode>0</AttributeCode>
        <AttributeValue>Du Frnt/Sd/Hd Air Bgs/Rr Hd Ar Bgs/Act Belts</AttributeValue>
    </RestraintsDriver>
    <BodyStyle>
        <AttributeCode>3095</AttributeCode>
        <AttributeValue>Sedan</AttributeValue>
    </BodyStyle>
    <Cab>
        <AttributeCode>0</AttributeCode>
        <AttributeValue></AttributeValue>
    </Cab>
    <AssemblyCountry>
        <AttributeCode>0</AttributeCode>
        <AttributeValue>Mexico</AttributeValue>
    </AssemblyCountry>
    <Cylinders>
        <AttributeCode>203</AttributeCode>
        <AttributeValue>4</AttributeValue>
    </Cylinders>
    <EngineDisp>
        <AttributeCode>218</AttributeCode>
        <AttributeValue>1.5 L</AttributeValue>
    </EngineDisp>
    <CombustEngManufacturer>
        <AttributeCode>0</AttributeCode>
        <AttributeValue></AttributeValue>
    </CombustEngManufacturer>
    <Fuel>
        <AttributeCode>194</AttributeCode>
        <AttributeValue>Gasoline</AttributeValue>
    </Fuel>
    <Make>
        <AttributeCode>184</AttributeCode>
        <AttributeValue>Ford</AttributeValue>
    </Make>
    <ModelYear>
        <AttributeCode>10786</AttributeCode>
        <AttributeValue>2019</AttributeValue>
    </ModelYear>
    <Model>
        <AttributeCode>3954</AttributeCode>
        <AttributeValue>Fusion</AttributeValue>
    </Model>
    <Vehicle>
        <AttributeCode>4672</AttributeCode>
        <AttributeValue>Passenger Car</AttributeValue>
    </Vehicle>
    <Series>
        <AttributeCode>395</AttributeCode>
        <AttributeValue>SE</AttributeValue>
    </Series>
    <Drivetrain>
        <AttributeCode>4677</AttributeCode>
        <AttributeValue>FWD</AttributeValue>
    </Drivetrain>
    <TransmissionType>
        <AttributeCode>207</AttributeCode>
        <AttributeValue></AttributeValue>
    </TransmissionType>
    <AxleWheelbase>
        <AttributeCode>0</AttributeCode>
        <AttributeValue>224.4</AttributeValue>
    </AxleWheelbase>
</DecodedVinResult>

我得到的 XML 是字符串的形式。这是我为解组编写的代码:

try {
            jaxbContext = JAXBContext.newInstance(type);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            response = jaxbUnmarshaller.unmarshal(new StringReader(xml));
        } catch (JAXBException e) {
            logger.error("=====Error while converting xml to pojo=======" + e.getMessage());
        }

这里是 POJO 类:

@XmlRootElement(name = "DecodedVinResult")
public class DecodedVinResult {

    private CombustEngManufacturer CombustEngManufacturer;

    private VINValidationResult VINValidationResult;

    private RestraintsDriver RestraintsDriver;

    private Cab Cab;

    private Make Make;

    private BrakesWeightRatingGVWR BrakesWeightRatingGVWR;

    private Vehicle Vehicle;

    private Series Series;

    private AssemblyCountry AssemblyCountry;

    private AxleWheelbase AxleWheelbase;

    private AssemblyPlant AssemblyPlant;

    private TransmissionType TransmissionType;

    private BodyStyle BodyStyle;

    private Fuel Fuel;

    private Model Model;

    private BrakesPower BrakesPower;

    private Drivetrain Drivetrain;

    private String VIN;

    private ModelYear ModelYear;

    private EngineDisp EngineDisp;

    private Cylinders Cylinders;

    public CombustEngManufacturer getCombustEngManufacturer () {
        return CombustEngManufacturer;
    }

    public void setCombustEngManufacturer (CombustEngManufacturer CombustEngManufacturer) {
        this.CombustEngManufacturer = CombustEngManufacturer;
    }

    public VINValidationResult getVINValidationResult () {
        return VINValidationResult;
    }

    public void setVINValidationResult (VINValidationResult VINValidationResult)
    {
        this.VINValidationResult = VINValidationResult;
    }

    public RestraintsDriver getRestraintsDriver ()
    {
        return RestraintsDriver;
    }

    public void setRestraintsDriver (RestraintsDriver RestraintsDriver)
    {
        this.RestraintsDriver = RestraintsDriver;
    }

    public Cab getCab ()
    {
        return Cab;
    }

    public void setCab (Cab Cab)
    {
        this.Cab = Cab;
    }

    public Make getMake ()
    {
        return Make;
    }

    public void setMake (Make Make)
    {
        this.Make = Make;
    }

    public BrakesWeightRatingGVWR getBrakesWeightRatingGVWR ()
    {
        return BrakesWeightRatingGVWR;
    }

    public void setBrakesWeightRatingGVWR (BrakesWeightRatingGVWR BrakesWeightRatingGVWR)
    {
        this.BrakesWeightRatingGVWR = BrakesWeightRatingGVWR;
    }

    public Vehicle getVehicle ()
    {
        return Vehicle;
    }

    public void setVehicle (Vehicle Vehicle)
    {
        this.Vehicle = Vehicle;
    }

    public Series getSeries ()
    {
        return Series;
    }

    public void setSeries (Series Series)
    {
        this.Series = Series;
    }

    public AssemblyCountry getAssemblyCountry ()
    {
        return AssemblyCountry;
    }

    public void setAssemblyCountry (AssemblyCountry AssemblyCountry)
    {
        this.AssemblyCountry = AssemblyCountry;
    }

    public AxleWheelbase getAxleWheelbase ()
    {
        return AxleWheelbase;
    }

    public void setAxleWheelbase (AxleWheelbase AxleWheelbase)
    {
        this.AxleWheelbase = AxleWheelbase;
    }

    public AssemblyPlant getAssemblyPlant ()
    {
        return AssemblyPlant;
    }

    public void setAssemblyPlant (AssemblyPlant AssemblyPlant)
    {
        this.AssemblyPlant = AssemblyPlant;
    }

    public TransmissionType getTransmissionType ()
    {
        return TransmissionType;
    }

    public void setTransmissionType (TransmissionType TransmissionType)
    {
        this.TransmissionType = TransmissionType;
    }

    public BodyStyle getBodyStyle ()
    {
        return BodyStyle;
    }

    public void setBodyStyle (BodyStyle BodyStyle)
    {
        this.BodyStyle = BodyStyle;
    }

    public Fuel getFuel ()
    {
        return Fuel;
    }

    public void setFuel (Fuel Fuel)
    {
        this.Fuel = Fuel;
    }

    public Model getModel ()
    {
        return Model;
    }

    public void setModel (Model Model)
    {
        this.Model = Model;
    }

    public BrakesPower getBrakesPower ()
    {
        return BrakesPower;
    }

    public void setBrakesPower (BrakesPower BrakesPower)
    {
        this.BrakesPower = BrakesPower;
    }

    public Drivetrain getDrivetrain ()
    {
        return Drivetrain;
    }

    public void setDrivetrain (Drivetrain Drivetrain)
    {
        this.Drivetrain = Drivetrain;
    }

    public String getVIN ()
    {
        return VIN;
    }

    public void setVIN (String VIN)
    {
        this.VIN = VIN;
    }

    public ModelYear getModelYear ()
    {
        return ModelYear;
    }

    public void setModelYear (ModelYear ModelYear)
    {
        this.ModelYear = ModelYear;
    }

    public EngineDisp getEngineDisp ()
    {
        return EngineDisp;
    }

    public void setEngineDisp (EngineDisp EngineDisp)
    {
        this.EngineDisp = EngineDisp;
    }

    public Cylinders getCylinders ()
    {
        return Cylinders;
    }

    public void setCylinders (Cylinders Cylinders)
    {
        this.Cylinders = Cylinders;
    }


}

代码不会引发任何错误,但 DecodedVinResult 类型的对象内的所有属性都为 null。我不知道我在这里做错了什么。

提前致谢

【问题讨论】:

    标签: java soap jaxb


    【解决方案1】:

    看来您必须在每个字段的前面添加@XmlElement

    @XmlRootElement(name = "DecodedVinResult")
    public class DecodedVinResult {
        @XmlElement
        private CombustEngManufacturer CombustEngManufacturer;
        @XmlElement
        private VINValidationResult VINValidationResult;
        ...
    }
    

    我还建议不要为每个节点创建一个新对象,因为它们都有相同的结构——只有两个字段AttributeCodeAttributeValue。只需添加一些统一的对象:

    class AbstractNode {
        @XmlElement
        private String AttributeCode;
        @XmlElement
        private String AttributeValue;
    
        // Getters and Setters
    }
    

    并将其用作对象类型:

    @XmlRootElement(name = "DecodedVinResult")
    class DecodedVinResult {
        @XmlElement
        private AbstractNode AssemblyPlant;
        @XmlElement
        private AbstractNode BrakesPower;
        ...
    
        // Getters and Setters
    }
    

    结果会是这样(见调试截图):

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多