Java – how to programmatically enable POJO mapping in Jersey using grizzly2?

According to 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 described here, but the problem is that I want to do it programmatically rather than through the web XML file (I don't have a web. XML file!) To achieve it

How to modify the above code to enable JSON POJO mapping?

Solution

protected static HttpServer startServer() throws IOException {
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);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>