无法使用Maven quickstart grizzly Archetype生成JSON

时间:2013-02-06 01:13:24

标签: json jersey maven-archetype grizzly

我花了一些时间试图让我的灰熊网络服务器生成JSON。

使用Intellij从maven原型生成,详情:

groupId untitled3
artifactId  untitled3
version 1.0-SNAPSHOT
archetypeGroupId    org.glassfish.jersey.archetypes
archetypeArtifactId jersey-quickstart-grizzly2
archetypeVersion    2.0-m05

所有在线示例都使用

启用JSON
rc.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

但是,这在泽西2.x中不起作用,put方法不存在。

在应用程序Main类中,有关于取消注释一行代码以使JSON工作的说明。当我取消注释这行代码时,使用的方法不存在。

public static HttpServer startServer() {
    // create a resource config that scans for JAX-RS resources and providers
    // in untitled2 package
    final ResourceConfig rc = new ResourceConfig().packages("untitled2");

    // uncomment the following line if you want to enable
    // support for JSON on the service (you also have to uncomment
    // dependency on jersey-media-json module in pom.xml)
    // --
    rc.addModule(org.glassfish.jersey.media.json.JsonJaxbModule);

    // create and start a new instance of grizzly http server
    // exposing the Jersey application at BASE_URI
    return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
}

当我尝试从POJO对象提供JSON响应时,我收到此错误:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.example.JsonPost, genericType=class com.example.JsonPost.

我不知道从哪里开始看起来真的。我用谷歌搜索,通过文档推动并查看用户组......

1 个答案:

答案 0 :(得分:2)

确保取消注释POM中的依赖项。然后将addModule调用更改为:

rc.addModules(new org.glassfish.jersey.media.json.JsonJaxbModule());

并确保在资源中包含正确的@Produces注释:

@Produces(MediaType.APPLICATION_JSON)
相关问题