如何使用smooks配置将json键值映射到java对象的枚举成员

时间:2019-02-20 15:01:08

标签: json smooks

如何使用烟雾将json映射到java对象。 我需要将json键值映射到java对象的枚举,并且如果键不存在,则从枚举中获取一些默认值。下面是json文件,java pojo和smooks配置文件。 预先感谢。

Json:

{
"orderId" : 1,
"username": "Pooja",
"priority": "HIGH",
"orderItems" : 
    {
        "quantity" : "1",
        "product id" : "364",
        "title" : "abc",
        "price" : "29.98"

    }
}

Java Pojo:

public class Simple {
private String orderId;
private String username;
private Map<String, String> orderItems = new HashMap<>();
public LineOrderPriority priority;
//getters and setters of above members
public enum LineOrderPriority {
HIGH("High"), MEDIUM("Medium"), NORMAL("Normal"), LOW("Low");
private final String value;
private LineOrderPriority(final String value) {
    this.value = value;
}
public String getValue() {
    return value;
} } }

smooks_config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list
xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd"
xmlns:json="http://www.milyn.org/xsd/smooks/json-1.1.xsd"
xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.4.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<json:reader rootName="simple">
</json:reader>
<jb:bean class="com.example.demo.Simple" beanId="simple"
    createOnElement="simple">
    <jb:value property="orderId" data="#/orderId" />
    <jb:value property="username" data="#/username" />
    <jb:value property="priority" data="#/priority"
        default="HIGH" decoder="Enum" />
    <jb:wiring property="orderItems" beanIdRef="orderItems" />
</jb:bean>
<jb:bean beanId="orderItems" class="java.util.HashMap"
    createOnElement="simple">
    <jb:value data="#/orderItems/*" />
</jb:bean>
</smooks-resource-list>

0 个答案:

没有答案