2012-02-28 2 views
6

는이 코드가 있습니다Grizzly2를 사용하여 Jersey에서 프로그래밍 방식으로 POJO 매핑을 사용하려면 어떻게해야합니까? 지침 <a href="http://jersey.java.net/nonav/documentation/latest/user-guide.html#getting-started" rel="noreferrer">here</a>에 따라

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

다음 내가 설명 here로 JSON POJO 지원을 활성화 할을하지만, 문제는 내가 (web.xml 파일을 통해 프로그래밍보다는 그것을 할 것입니다 web.xml 파일이 없습니다!).

JSON POJO 매핑 기능을 사용하려면 위의 코드를 어떻게 수정할 수 있습니까?

답변

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

jersey-json 라이브러리를 프로젝트에 추가하기 만하면됩니다. 심지어 ResourceConfig의 feautres에 JSONConfiguration.FEATURE_POJO_MAPPING를 추가하지 않고

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

이 나를 위해 작동합니다

당신이 받는다는을 사용하는 경우, 그냥이 종속성을 추가.