简体   繁体   中英

Can't find ScalaSig for class

Throughout modules in my project I am using core library written in Java . One of the modules has been written in Scala ( Scalatra framework).

At some point I need use core library's class: net.example.MyClass , and serialize it to String. In modules written in Java, this works just fine, but in Scala I'm encountering following exception:

org.json4s.package$MappingException: Can't find ScalaSig for class net.example.MyClass
at org.json4s.reflect.ScalaSigReader$.findClass(ScalaSigReader.scala:42) ~[json4s-core_2.11-3.2.10.jar:3.2.10]
at org.json4s.reflect.ScalaSigReader$.org$json4s$reflect$ScalaSigReader$$read$1(ScalaSigReader.scala:36) ~[json4s-core_2.11-3.2.10.jar:3.2.10]
...

net.exmample.MyClass is created using Builder pattern, annotation used: @JsonDeserialize(builder = MyClass.Builder.class).

I imagine this might do something with reflection or the @JsonDeserialize annotation not being processed. Has anybody encountered similar error, is there solution for this, or the documentation I could check? Any help would be appreciated.

Thanx!

Using:
- Java 8
- Scala 2.11.0
- Scalatra 2.3.0
- json4s-jackson 3.2.10 for serialization to String

Finally, I have resolved the error by changing how the JSON value is created. Instead of using the json4s wrapper:

org.json4s.jackson.Serialization.write()

I switched to using Jackson mapper directly:

com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString()

Edit : In my ScalatraBootstrap.init() method, I have defined the Jackson's ObjectMapper :

import com.fasterxml.jackson.databind.ObjectMapper

class ScalatraBootstrap extends LifeCycle {

    override def init(context: ServletContext) {

        implicit val objectMapper = new ObjectMapper

        //omitting the rest of the code...
    }
    ...
}

And here is my service class where I'm fetching the net.example.MyClass and transforming it to JSON with the ObjectMapper :

import net.example.MyClass
import com.fasterxml.jackson.databind.ObjectMapper

class MyService(val objectMapper: ObjectMapper) {

    def getMyClassAsJson() {
        val result: MyClass = // calling a service which provides the MyClass object
        return objectMapper.writeValueAsString(result)
    }
}

you post the json including net.example.MyClass[] . As MyClass[] is a array, the ScalaSig cannot resolved object array, so you should use ArrayList<net.example.MyClass> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM