简体   繁体   English

Jersey 2.5中的Scala JSON序列化支持

[英]Scala JSON serialization support in Jersey 2.5

I have created a Jersey 2.5 Scala REST API Project. 我已经创建了Jersey 2.5 Scala REST API项目。

I have a ResourceConfig file, we will call it MyApplication , that looks similar to this: 我有一个ResourceConfig文件,我们将其称为MyApplication ,其外观类似于此:

class MyApplication extends ResourceConfig {
    packages(classOf[MyResource].getPackage().getName())
}

All it does is register the resource: MyResource . 它所做的只是注册资源: MyResource How can I configure Jersey (2.5) to provide out-of-the-box style JSON Serialization/Deserialization. 我如何配置Jersey(2.5)以提供即用型的JSON序列化/反序列化。

For example, here is what MyResource might look like: 例如,这是MyResource可能的样子:

@Path("/")
class MyResource {

    @POST
    @Produces(Array("application/json"))
    @Consumes(Array("application/json"))
    def getIt(request:SomeRequestModel) = {
        /* Do something with the request, return some response model */
        return new SomeResponseModel
    }

}

So to reiterate, how can I configure Jersey to automatically deserialize and serialize the request and response models, respectively? 因此,重申一下,如何配置Jersey分别分别自动反序列化和序列化请求和响应模型?

It's not actually Jersey that provides the serialisation, it simply draws on an implementation of JAX-RS to perform that role. 提供序列化的实际上不是Jersey,它只是依靠JAX-RS的实现来执行该角色。

Assuming Jersey is a strict requirement, the easiest solution here is to use jackson with Scala bindings. 假设Jersey是一个严格的要求,那么最简单的解决方案是将Jackson与Scala绑定一起使用。 You can find an example here: https://bitbucket.org/jordipradel/jersey-scala-example 您可以在此处找到示例: https : //bitbucket.org/jordipradel/jersey-scala-example

If you're not completely tied to Jersey... Might I suggest trying either Spray or spray2-mini instead for a far more idiomatic Scala solution? 如果您不完全依赖Jersey ...我可能建议尝试使用Spray或spray2-mini替代更通用的Scala解决方案?

I have used Jersey little when developing Java REST services. 开发Java REST服务时,我很少使用Jersey。 However, I would say you appear to be conflating two concepts--registering JAX-RS providers and configuring providers to serialize/deserialize JSON. 但是,我想说您似乎正在混淆两个概念-注册JAX-RS提供程序和配置提供程序以序列化/反序列化JSON。

To register a provider, you use ResourceConfig as you have done. 要注册提供者,请像完成操作一样使用ResourceConfig

As for the second issue of configuring Jersey to "know" how to serialize/deserialize JSON: 至于配置Jersey以“知道”如何序列化/反序列化JSON的第二个问题:

" As stated in Section 4.3, “Auto-Discoverable Features” JSON-Processing media module is one of the modules where you don't need to explicitly register it's Features (JsonProcessingFeature) in your client/server Configurable as this feature is automatically discovered and registered when you add jersey-media-json-processing module to your classpath. " 如第4.3节所述,“自动发现功能” JSON-Processing媒体模块是不需要在客户端/服务器中明确注册其功能(JsonProcessingFeature)的模块之一,可配置为自动发现并自动在将jersey-media-json-processing模块添加到类路径中时注册。

To add the Jackson flavor of that module to the classpath, you just manually put it there or do this with Maven: 要将该模块的杰克逊风格添加到类路径中,只需手动将其放在那里或使用Maven进行即可:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.5</version>
</dependency> 

I chose Jackson because it is in my view the best JSON serializer/deserializer in the Java realm. 我选择Jackson是因为它是Java领域中最好的JSON序列化器/反序列化器。

Having said all this, I once experimented writing services with my preferred Java REST framework, RESTEasy, in Scala. 综上所述,我曾经在Scala中尝试使用首选的Java REST框架RESTEasy编写服务。 It was more awkward than my first date. 比我第一次约会更尴尬。 Scala and RESTEasy just don't fit together because of an idiom mismatch, issues with types, and so on. 由于习语不匹配,类型问题等,Scala和RESTEasy不能一起使用。

If you want to write REST services in Scala, please consider frameworks built with the language in mind like Scalatra , Unfiltered , or Spray . 如果要用Scala编写REST服务,请考虑使用诸如ScalatraUnfilteredSpray之类的语言构建的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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