javajaxb::解组SOAP响应并在嵌套对象中获取null
调用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。我不知道这里是否有我做错了什么
提前谢谢
# 1 楼答案
看起来您必须在每个字段前面添加
@XmlElement
我还建议不要为每个节点创建新对象,因为它们都具有相同的结构-只有两个字段
AttributeCode
和AttributeValue
。只需添加一些统一对象:并将其用作对象类型:
结果如下所示(请参见调试的屏幕截图):