Search code examples
scalajaxbbigdecimal

Problem in serialization "scala.math.BigDecimal does not have a no-arg default constructor"


I'm new to scala programming and java so here is my problem:

I have an object to be serialized with a BigDecimal Property

import java.util.Date
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter
import reflect.BeanProperty

class TestObject {
  @XmlJavaTypeAdapter(classOf[BigDecimalAdapter])
  var test: BigDecimal = 0.00
}

I receive this error:

scala.math.BigDecimal does not have a no-arg default constructor

XmlAdapter:

import javax.xml.bind.annotation.adapters.XmlAdapter

class BigDecimalAdapter extends XmlAdapter[String, BigDecimal] {
  def unmarshal(str: String) = BigDecimal(str)
  def marshal(bD: BigDecimal) = bD.toString()
}

SOAPServer:

import javax.jws.soap.SOAPBinding
import javax.jws.{WebParam, WebMethod, WebService}
import javax.xml.ws.Endpoint

@WebService(targetNamespace="test", name="testws", portName="test", serviceName="wsTest")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
class Server {
  @WebMethod(action = "test")
  def test(@WebParam(name = "testParam") testParam:TestObject): TestObject = {
    return testParam
  }

}

object SoapServer { // defined Companion Object for our class
  def main(args: Array[String]) { // main method to make this a runnable application
    val endpoint = Endpoint.publish("http://192.168.189.132:8080/wsTest", new Server())
    System.out.println("Binded to port 8080. Waiting for requests...")
  }
}

Solution

  • I think you probably want to be using java.math.BigDecimal, as opposed to scala.math.BigDecimal. Use the fully-qualified path name:

     import java.math.{BigDecimal => JDec}
     var test: JDec = new JDec(0)
    

    It seems like the jaxb framework is expecting a no-arg constructor; I'm not familiar enogh with it to understand why