2012-02-28 34 views
6

Tiếp theo các hướng dẫn here tôi có mã này:làm cách nào để kích hoạt lập trình POJO lập trình trong Jersey bằng 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(); 
} 

Tiếp theo, tôi muốn cho phép hỗ trợ JSON POJO như mô tả here, nhưng vấn đề là tôi muốn làm điều đó theo chương trình chứ không phải thông qua một file web.xml (Tôi không có tệp web.xml!).

Làm cách nào để sửa đổi mã ở trên để bật tính năng lập bản đồ JSON POJO?

Trả lời

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

Bạn chỉ cần thêm thư viện jersey-json vào dự án của mình.

Nếu bạn đang sử dụng maven, chỉ cần thêm sự phụ thuộc này:

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

này làm việc cho tôi thậm chí không có thêm JSONConfiguration.FEATURE_POJO_MAPPING để feautres của ResourceConfig.

Các vấn đề liên quan