从jaxb的请求解析xml时,字段中的值为空?

时间:2018-10-18 08:34:09

标签: xml spring jaxb

我在spring + maven + jaxb上有过申请。 有休息控制器

@RequestMapping(value = "/risks", method = RequestMethod.POST)
    public void addRisks(@RequestBody RiskMetric risks) {
        risksService.addriks(risks);
    }

需要像这样将xml解析为实体Risk

<Risks>
  <test>111</test>
  <param>
    <type>testType</type>
  /param>
  <currency>UYU</currency>
</Risks>

但是,在创建的风险实体中,字段类型为null。风险实体:

@XmlRootElement(name = "Risks")
@XmlAccessorType(XmlAccessType.FIELD)
public class Risks implements Serializable {
    @XmlElement
    private String test;

    @XmlElement
    @XmlJavaTypeAdapter(ParamAdapter.class)
    private Param param;

    @XmlJavaTypeAdapter(CurrencyAdapter.class)
    @XmlElement
    private Currency currency;
    ...constructors and getters\setters..

ParamAdapter是:

public class ParamAdapter extends XmlAdapter<String, Param> {

    @Override
    public Param unmarshal(String v) throws Exception {
        return new Param(v);
    }

    @Override
    public String marshal(Param v) throws Exception {
        StringBuilder result = new StringBuilder();
        result.append(v.getType());
        return result.toString();
    }
}

和参数-类为:

public class Param implements Serializable {

    @XmlElement
    private String type;
...construcors and getters and setters..
}

我是否需要为Param类创建其他适配器,或者有其他方法来解决此问题?

p.s。在调试流下是访问解组方法,输入的字符串参数为“ \ n”-空字符串

1 个答案:

答案 0 :(得分:0)

删除了所有fast.xml依赖项并添加了jaxb。问题解决了。