簡體   English   中英

具有貨幣/十進制類型的 spring-boot Web 服務

[英]spring-boot web service with money/decimal type

我有一個帶有soap webservice的簡單spring-boot應用程序:
https://spring.io/guides/gs/production-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>
  // 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM