2013-02-06 51 views
1

我已經嘗試讓我的灰熊網絡服務器產生JSON。無法使用Maven快速入門生成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的工作使,把方法不存在。

在應用程序主類中,有關於取消註釋一行代碼以使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. 

我不知道從哪裏開始看真。我用google搜索了一遍,通過文檔查看並查看了用戶組...

回答

2

確保您取消註釋POM中的依賴關係。然後addModule調用改成這樣:

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

,並一定要包括你的資源的正確@Produces註釋:

@Produces(MediaType.APPLICATION_JSON) 
+0

這是soooooo方便,很明顯,當你知道如何做到這一點。我很認真,我找不到這個信息 - 任何地方。 – OakNinja

+0

非常感謝您花時間幫助我! – OakNinja