jjzjj

java - 对象必须在其@XmlValue 字段中有一些值

coder 2024-04-02 原文

我有以下代码

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "udt_TextType", propOrder = { "value" })
@XmlSeeAlso({ RoadTypeCodeTypeType.class })
public class UdtTextType {

@XmlValue
@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
@XmlSchemaType(name = "normalizedString")
protected String value;

/**
 * Gets the value of the value property.
 * 
 * @return possible object is {@link String }
 * 
 */
public String getValue() {
    return value;
}

/**
 * Sets the value of the value property.
 * 
 * @param value
 *            allowed object is {@link String }
 * 
 */
public void setValue(String value) {
    this.value = value;
}

当我尝试编码这个对象时:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "AdresseCompleteType", propOrder = { "inHouseMail", "buildingName", "buildingNumber", "blockName", "roadType",
        "streetName", "postcode", "cityName", "lineFive" })
public class AdresseCompleteType {

    @XmlElement(name = "InHouseMail")
    protected UdtTextType inHouseMail;
    @XmlElement(name = "BuildingName")
    protected UdtTextType buildingName;
    @XmlElement(name = "BuildingNumber")
    protected UdtTextType buildingNumber;
    ...

我有以下错误: 对象必须在其@XmlValue 字段中有一些值

为什么会出现此错误?

com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: fr.company.jaxb.UdtTextType@17f2dd8] at 
    fr.company.Service.launchXML(Service.java:303) at 
    fr.company.Service.launchZIP(Service.java:370) at 
    fr.company.MainTest.testGenerationZip(MainTest.java:17) at 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57))

谢谢!

最佳答案

下面解释了为什么会出现异常:

Java 模型

以下是您的模型的简化版本,用于演示问题:

UdtTextType

这个类有一个用@XmlValue注释的字段:

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class UdtTextType {

    @XmlValue
    protected String value;

    public void setValue(String value) {
        this.value = value;
    }

}

AdreseCompleteType

需要注意的重要一点是inHouseMail 字段是一个与@XmlValue 有映射关系的类型。

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AdreseCompleteType {

    @XmlElement(name = "InHouseMail")
    protected UdtTextType inHouseMail;

    public void setInHouseMail(UdtTextType inHouseMail) {
        this.inHouseMail = inHouseMail;
    }

}

XML 表示

让我们检查对象模型可能产生的不同 XML 表示。

#1 - inHouseMail 属性为 null

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(null);

在 XML 表示中,inHouseMail 字段应为 null,我不会在生成的 XML 中包含该元素。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType/>

#2 - inHouseMail 属性已设置且 UdtTextType 具有值

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("Hello World");

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

由于设置了 inHouseMail 字段,我们得到了 InHouseMail 元素,并且由于它的 value 字段被填充了,所以它也被输出了。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType>
    <InHouseMail>Hello World</InHouseMail>
</adreseCompleteType>

#3 - inHouseMail 属性已设置且 UdtTextType 具有空字符串值

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue("");

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

由于设置了 inHouseMail 字段,我们得到了 InHouseMail 元素,并且由于它的 value 字段被填充为一个空的 String 作为空元素输出。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<adreseCompleteType>
    <InHouseMail></InHouseMail>
</adreseCompleteType>

#4 - inHouseMail 属性已设置且 UdtTextType 具有 null

UdtTextType udtTextType = new UdtTextType();
udtTextType.setValue(null);

AdreseCompleteType adreseCompleteType = new AdreseCompleteType();
adreseCompleteType.setInHouseMail(udtTextType);

问题是这个 XML 应该是什么样子。它不能是像#3 这样的空元素,因为 JAXB(和 XML)不将 null 表示为空元素。它也不能表示为缺少的 InHouseMail 元素,因为那样会与场景 #1 发生冲突。所以抛出以下异常:

Exception in thread "main" javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5]
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
    at forum28411016.Demo.main(Demo.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: com.sun.istack.internal.SAXException2: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:235)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:250)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:347)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:582)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:325)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
    ... 8 more
Caused by: com.sun.xml.internal.bind.api.AccessorException: Object must have some value in its @XmlValue field: forum28411016.UdtTextType@f7ca0f5
    at com.sun.xml.internal.bind.v2.model.impl.RuntimeClassInfoImpl$TransducerImpl.writeLeafElement(RuntimeClassInfoImpl.java:395)
    at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor$CompositeTransducedAccessorImpl.writeLeafElement(TransducedAccessor.java:239)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementLeafProperty.serializeBody(SingleElementLeafProperty.java:114)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343)
    ... 12 more

关于java - 对象必须在其@XmlValue 字段中有一些值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28411016/

有关java - 对象必须在其@XmlValue 字段中有一些值的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  6. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  7. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  8. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  9. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

  10. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

随机推荐