6

按照此处的说明,我有以下代码:

private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost/").port(9998).build();
}

public static final URI BASE_URI = getBaseURI();

protected static HttpServer startServer() throws IOException {
    System.out.println("Starting grizzly...");
    final ResourceConfig rc = new PackagesResourceConfig("amplify.api.resources");
    return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
}

public static void main(final String[] args) throws IOException {
    final HttpServer httpServer = startServer();
    System.out.println(String.format("Jersey app started with WADL available at "
            + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...", BASE_URI, BASE_URI));
    System.in.read();
    httpServer.stop();
}

接下来我想启用 JSON POJO 支持,如此所述,但问题是我想以编程方式而不是通过 web.xml 文件(我没有 web.xml 文件!)。

如何修改上面的代码以启用 JSON POJO 映射功能?

4

2 回答 2

10
protected static HttpServer startServer() throws IOException {
    System.out.println("Starting grizzly...");
    final ResourceConfig rc = new PackagesResourceConfig("amplify.api.resources");
    rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);
    return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
}
于 2012-02-29T00:06:08.493 回答
2

您只需要将 jersey-json 库添加到您的项目中。

如果您使用的是 maven,只需添加此依赖项:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>${jersey.version}</version>
</dependency>

即使没有将 JSONConfiguration.FEATURE_POJO_MAPPING 添加到 ResourceConfig 的功能,这也适用于我。

于 2013-01-17T06:14:59.373 回答