2016-05-16 6 views
0

私は、Mule 3.6のワークフローでdatamapperに従うオブジェクトトランスフォーマーにJSONを渡して、JSONからオブジェクトへのトランスフォーマーが末尾のゼロを10進フィールドで削除して11.00になります。JSONからオブジェクトトランスフォーマへの丸め問題

JSON to 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 
+0

私はjacksonオブジェクトマッパーを使用して2桁の小数点以下を保持できますが、値を文字列に変更することができました。 jacksonオブジェクトマッパーを使用する方法がありますが、小数値としてフィールド値を保持しますか? – user3165854

答えて

0

あなたが使用することができ、あなたのデータを丸め、独自のカスタム関数を作成することができ

の例では、問題を示す流れ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(); 
} 

このコードから、関数を直接呼び出して正しく丸めることができます。

これが役に立ちます。

+0

お返事ありがとうございます。この問題は、JSONからObjectへの変換で発生します。 datamapperからの出力は正しい丸めを示しますが、JSONからObjectへの変換が実行されると、最後の小数点以下の桁が失われます。 – user3165854