【问题标题】:spring-boot web service with money/decimal type具有货币/十进制类型的spring-boot Web服务
【发布时间】:2016-03-11 23:44:17
【问题描述】:

我有一个简单的带有soap webservice的spring-boot应用程序:
https://spring.io/guides/gs/producing-web-service/
在 xsd 中,我添加了自定义 bigdecimal 类型(货币类型)。

     <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string" />
            <xs:element name="population" type="xs:int" />
            <xs:element name="capital" type="xs:string" />
            <xs:element name="currency" type="tns:currency" />
            <xs:element name="value1" type="xs:decimal" />
            <xs:element name="value2" type="tns:money" />
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="money">
        <xs:restriction base="xs:decimal">
            <xs:fractionDigits value="2" />
        </xs:restriction>
    </xs:simpleType>

CountryRepository 类:

    Country spain = new Country();
    spain.setName("Spain");
    spain.setCapital("Madrid");
    spain.setCurrency(Currency.EUR);
    spain.setPopulation(46704314);
    spain.setValue1(new BigDecimal(1.2));
    spain.setValue2(new BigDecimal(2.1));

    countries.add(spain);

请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gs="http://spring.io/guides/gs-producing-web-service">
   <soapenv:Header/>
   <soapenv:Body>
      <gs:getCountryRequest>
         <gs:name>Spain</gs:name>
      </gs:getCountryRequest>
   </soapenv:Body>
</soapenv:Envelope>

回复:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:getCountryResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
         <ns2:country>
            <ns2:name>Spain</ns2:name>
            <ns2:population>46704314</ns2:population>
            <ns2:capital>Madrid</ns2:capital>
            <ns2:currency>EUR</ns2:currency>
            <ns2:value1>1.1999999999999999555910790149937383830547332763671875</ns2:value1>
            <ns2:value2>2.100000000000000088817841970012523233890533447265625</ns2:value2>
         </ns2:country>
      </ns2:getCountryResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何修复十进制输出? 我想要:

<ns2:value2>2.10</ns2:value2>

【问题讨论】:

  • 您需要专门的映射器来执行此操作:docs.spring.io/spring/docs/current/spring-framework-reference/…
  • 调用BigDecimal(double) 构造函数会丢失精度。阅读清楚地强调这一点的构造函数的 JavaDocs。如果您暂时只是创建一个示例应用程序,您可以使用BigDecimal(String) 构造函数作为new BigDecimal("1.2")。对于实际应用程序,最好将BigDecimal 包装到特定于域的类中,例如Money,并在自定义类中应用所需的格式。
  • @manish:谢谢。你说的对。问题在于双重类型。我已经用从 oracle 数据库中获取的 BigDecimal 替换了它,现在可以了。
  • @Marged:你有什么例子吗?不幸的是,jaxb2-maven-plugin 没有在 value2 上生成任何注释(value1 和 value2 是相同的 BigDecimal 类型)。如果我想在架构级别控制大十进制的精度怎么办……这可能吗?

标签: java spring web-services spring-boot spring-ws


【解决方案1】:
  // step 1
  BigDecimal valueOne = new BigDecimal(2.1);
  BigDecimal valueTwo = new BigDecimal(2.1);

  // step 2
  valueOne.setScale(2);
  valueTwo.setScale(2);

  //step 3
  spain.setValue1(new BigDecimal(1.2));
  spain.setValue2(new BigDecimal(2.1));

  //console -> valueOne -> 1.2
  //console -> valueTwo -> 2.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多