在执行JAXB Unmarshalling时处理无效的枚举值

sri*_*ram 9 web-services soapui jaxb2

我的Jaxb基于XML模式设置创建了一个Enum类.

**enum Fruit {
    APPLE,ORANGE;
}**
Run Code Online (Sandbox Code Playgroud)

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

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

关于sri

bdo*_*han 18

注意: 我是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;
    }

}
Run Code Online (Sandbox Code Playgroud)

水果

package forum12147306;

public enum Fruit {

    APPLE, 
    ORANGE;

}
Run Code Online (Sandbox Code Playgroud)

演示

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));
    }

}
Run Code Online (Sandbox Code Playgroud)

JAXB参考实施

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

Not a number: ABC
Run Code Online (Sandbox Code Playgroud)

解决

编写自己的代码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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

水果

使用@XmlJavaTypeAdapter注释将XmlAdapterFruitenumb 关联.

package forum12147306;

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

@XmlJavaTypeAdapter(FruitAdapter.class)
public enum Fruit {

    APPLE, 
    ORANGE;

}
Run Code Online (Sandbox Code Playgroud)

新产出

Not a number: ABC
javax.xml.bind.JAXBException
 - with linked exception:
[java.lang.IllegalArgumentException: No enum const class forum12147306.Fruit.Guva]
Run Code Online (Sandbox Code Playgroud)

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)])
Run Code Online (Sandbox Code Playgroud)