找不到类的 ScalaSig [英] Can't find ScalaSig for class

查看:51
本文介绍了找不到类的 ScalaSig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我项目中的整个模块都使用以 Java 编写的核心库.其中一个模块是用 Scala(Scalatra 框架)编写的.

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

有时我需要使用核心库的类:net.example.MyClass,并将其序列化为字符串.在用 Java 编写的模块中,这工作得很好,但在 Scala 中,我遇到了以下异常:

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.exmaple.MyClass 是使用 Builder 模式创建的,使用的注释:@JsonDeserialize(builder = MyClass.Builder.class).

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

我想这可能会通过反射或未处理的@JsonDeserialize 注释来做一些事情.有没有人遇到过类似的错误,有没有解决方案,或者我可以检查的文档?任何帮助将不胜感激.

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.

谢谢!

使用:
- Java 8
- Scala 2.11.0
- Scalatra 2.3.0
- json4s-jackson 3.2.10 用于序列化为字符串

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

推荐答案

最后,我通过更改 JSON 值的创建方式解决了该错误.而不是使用 json4s 包装器:

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()

我直接改用 Jackson 映射器:

I switched to using Jackson mapper directly:

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

编辑:在我的 ScalatraBootstrap.init() 方法中,我定义了 Jackson 的 ObjectMapper:

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...
    }
    ...
}

这是我的服务类,我在其中获取 net.example.MyClass 并使用 ObjectMapper 将其转换为 JSON:

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)
    }
}

这篇关于找不到类的 ScalaSig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆