【问题标题】:Mule rounding issue with JSON to object transformerJSON到对象转换器的Mule舍入问题
【发布时间】:2016-09-12 01:46:03
【问题描述】:

我有一个 JSON 到对象转换器,它遵循 Mule 3.6 工作流程中的数据映射器,发现 JSON 到对象转换器删除了十进制字段中的尾随零,因此 11.00 变为 11.0。

如果 JSON 到 Object 转换器的最后一个小数位为零,我如何防止它删除最后一个小数位并强制保留 2 个小数位,以便返回 11.00,因为这是一个要求?

显示问题的示例流程:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <data-mapper:config name="CSV_To_JSON" transformationGraphPath="csv_to_json.grf" doc:name="CSV_To_JSON"/>
    <flow name="deimal-point-flowFlow">
        <file:inbound-endpoint path="${file.unprocessed.location}" moveToPattern="#[message.inboundProperties['originalFilename']]" moveToDirectory="${file.unprocessed.location}" responseTimeout="10000" doc:name="File">
            <file:filename-regex-filter pattern="test.csv" caseSensitive="true"/>
        </file:inbound-endpoint>
        <data-mapper:transform config-ref="CSV_To_JSON" doc:name="CSV To JSON"/>
        <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
        <json:object-to-json-transformer returnClass="java.lang.String" mimeType="application/json" doc:name="Object to JSON"/>
        <logger level="INFO" doc:name="Logger"/>
    </flow>
</mule>

test.csv 文件如下所示:

DeptID,Dept,Staff
5LL/A,Human Resources,4.00

【问题讨论】:

  • 我发现我可以使用 jackson 对象映射器来保留小数点后 2 位,但它会将值更改为字符串。有没有办法使用杰克逊对象映射器但将字段值保留为小数?

标签: mule rounding transformer anypoint-studio


【解决方案1】:

您可以创建自己的自定义函数来对数据进行四舍五入,供 datamapper 使用。首先在您的流程上添加一个配置,以扩展表达式语言。

  <configuration doc:name="Configuration">
     <expression-language autoResolveVariables="true">
      <import name="dataformatter" class="com.utils.DataFormatter" />
      <global-functions >
          def (value, places) { return dataformatter.round(value, places) }
       </global-functions>
      </expression-language>
    </configuration>



//package com.utils.DataFormatter
public static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();
    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

从该代码中,您可以直接调用该函数并对其进行正确舍入。

希望这会有所帮助。

【讨论】:

  • 感谢您的回复。问题在于 JSON 到对象转换器。数据映射器的输出显示了正确的舍入,但是当 JSON 到对象转换器起作用时,它会丢失最后一个小数位。
猜你喜欢
  • 1970-01-01
  • 2014-02-18
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
相关资源
最近更新 更多