Java 如何使用Grizzly2在Jersey中以编程方式启用POJO映射?

Java 如何使用Grizzly2在Jersey中以编程方式启用POJO映射?,java,jersey,grizzly,Java,Jersey,Grizzly,按照说明,我有以下代码: 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.p

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

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映射功能?

您只需将JSON库添加到项目中即可

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);
}
如果您使用的是maven,只需添加以下依赖项:

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

泽西岛
泽西json
${jersey.version}
即使没有将JSONConfiguration.FEATURE\u POJO\u映射添加到ResourceConfig的feautres中,这对我来说也是有效的