2012-02-28 17 views
6

Postępując zgodnie z instrukcjami here mam ten kod:Jak włączyć programowanie POJO programowo w Jersey przy użyciu 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(); 
} 

Następny chcę włączyć obsługę JSON POJO jak opisano here, ale problemem jest to, że chcę to zrobić programowo, a nie za pośrednictwem pliku web.xml (Nie mam pliku web.xml!).

Jak mogę zmienić powyższy kod, aby włączyć funkcję mapowania JSON POJO?

Odpowiedz

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

Musisz tylko dodać bibliotekę jersey-json do swojego projektu.

Jeśli używasz Maven, po prostu dodaj tę zależność:

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

Działa to dla mnie nawet bez dodawania JSONConfiguration.FEATURE_POJO_MAPPING do feautres w ResourceConfig użytkownika.

Powiązane problemy