Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I enable POJO-mapping programatically in Jersey using Grizzly2?

Following the instructions here I have this code:

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

Next I want to enable JSON POJO support as described here, but the problem is that I want to do it programmatically rather than through a web.xml file (I don't have a web.xml file!).

How can I modify the code above to enable the JSON POJO mapping feature?

like image 543
sanity Avatar asked Feb 28 '12 21:02

sanity


2 Answers

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);
}
like image 106
Pavel Bucek Avatar answered Nov 15 '22 01:11

Pavel Bucek


You just need to add jersey-json library to your project.

If you are using maven, just add this dependency:

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

This works for me even without adding JSONConfiguration.FEATURE_POJO_MAPPING to the ResourceConfig's feautres.

like image 37
ely Avatar answered Nov 15 '22 00:11

ely