2012-02-28 19 views
6

Siguiendo las instrucciones here tengo este código:¿cómo habilito POJO-mapping programáticamente en Jersey usando Grizzly2?

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(); 
} 

Siguiente Quiero habilitar el soporte JSON POJO como se describe here, pero el problema es que yo quiero hacerlo mediante programación en lugar de a través de un archivo web.xml (¡No tengo un archivo web.xml!).

¿Cómo puedo modificar el código anterior para habilitar la función de mapeo JSON POJO?

Respuesta

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); 
} 
2

sólo tiene que añadir la biblioteca Jersey-JSON para su proyecto.

Si está utilizando Maven, sólo tiene que añadir esta dependencia:

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

Esto funciona para mí, incluso sin añadir JSONConfiguration.FEATURE_POJO_MAPPING a feautres del ResourceConfig.

Cuestiones relacionadas