web-services – 在执行JAXB Unmarshalling时处理无效的枚举值

前端之家收集整理的这篇文章主要介绍了web-services – 在执行JAXB Unmarshalling时处理无效的枚举值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的Jaxb基于XML模式设置创建了一个Enum类.
**enum Fruit {
    APPLE,ORANGE;
}**

我正在使用SOAP UI来检查我的Web服务.因为它是一个自由形式的条目,如果我给错误的水果说“Guva”然后而不是抛出异常,它在做UnMarshalling之后将它返回为null.

我怎能避免这种情况?我应该使用自定义枚举类而不是JAXB生成的.请举个例子.即

问候
SRI

解决方法

注意:我是 EclipseLink JAXB (MOXy)领导者,也是 JAXB (JSR-222)专家组的成员.

默认情况下,您的JAXB(JSR-222)实现不会在任何转换异常上失败.如果您使用JAXB API进行解组,则可以设置ValidationEventHandler来捕获任何问题.以下是一个例子.

package forum12147306;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Root {

    private int number;
    private Fruit fruit;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public Fruit getFruit() {
        return fruit;
    }

    public void setFruit(Fruit fruit) {
        this.fruit = fruit;
    }

}

水果

package forum12147306;

public enum Fruit {

    APPLE,ORANGE;

}

演示

package forum12147306;

import java.io.StringReader;
import javax.xml.bind.*;

public class Demo {

    private static final String XML = "<root><number>ABC</number><fruit>Guva</fruit></root>";
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent validationEvent) {
                 System.out.println(validationEvent.getMessage());
                 //validationEvent.getLinkedException().printStackTrace();
                 return true;
            }

        });

        Root root = (Root) unmarshaller.unmarshal(new StringReader(XML));
    }

}

JAXB参考实施

不幸的是,JAXB RI中似乎存在一个错误,因为无效的枚举值没有通过验证事件.

Not a number: ABC

解决

编写自己的XmlAdapter来处理与Fruit枚举的转换:

FruitAdapter

package forum12147306;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class FruitAdapter extends XmlAdapter<String,Fruit> {

    @Override
    public String marshal(Fruit fruit) throws Exception {
        return fruit.name();
    }

    @Override
    public Fruit unmarshal(String string) throws Exception {
        try {
            return Fruit.valueOf(string);
        } catch(Exception e) {
            throw new JAXBException(e);
        }
    }

}

水果

使用@XmlJavaTypeAdapter批注将XmlAdapter与Fruit enumb相关联.

package forum12147306;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlJavaTypeAdapter(FruitAdapter.class)
public enum Fruit {

    APPLE,ORANGE;

}

新产出

Not a number: ABC
javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.IllegalArgumentException: No enum const class forum12147306.Fruit.Guva]

EclipseLink JAXB(MOXy)

使用MOXy抛出两个验证事件.要将MOXy指定为JAXB提供程序,请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html.

Exception Description: The object [ABC],of class [class java.lang.String],from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[number-->number/text()]] with descriptor [XMLDescriptor(forum12147306.Root --> [DatabaseTable(root)])],could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "ABC"

Exception Description: No conversion value provided for the value [Guva] in field [fruit/text()].
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[fruit-->fruit/text()]
Descriptor: XMLDescriptor(forum12147306.Root --> [DatabaseTable(root)])
原文链接:https://www.f2er.com/html/232227.html

猜你在找的HTML相关文章