2012-02-28 20 views
6

Bu kodu vardır:Grizzly2 kullanarak Jersey'de POJO-mapping'i programatik olarak nasıl etkinleştiririm? Talimatlar <a href="http://jersey.java.net/nonav/documentation/latest/user-guide.html#getting-started" rel="noreferrer">here</a> ardından

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

Sonraki Ben tarif here olarak JSON POJO desteğini etkinleştirmek istiyorum ama sorun ben (bir web.xml dosyası üzerinden programlı ziyade yapmak istiyorum olmasıdır Bir web.xml dosyası yok!).

JSON POJO eşleme özelliğini etkinleştirmek için yukarıdaki kodu nasıl değiştirebilirim?

cevap

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

Projenize jersey-json kitaplığı eklemeniz yeterlidir. Hatta ResourceConfig en feautres için JSONConfiguration.FEATURE_POJO_MAPPING eklemeden

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

Bu benim için çalışıyor:

sen maven kullanıyorsanız, sadece bu bağımlılığı ekleyin.