2013-04-02 19 views
7

Grizzly üzerinde çalışan bir Servlet'te bazı enjekte edilmiş bağlamı (örneğin, Session veya HttpServletRequest) tutmaya çalışıyorum, ancak yaptığım hiçbir şey işe yaramıyor gibi görünüyor.Grizzly ve ServletContainerContext

package com.test.server; 

import java.io.IOException; 
import java.net.URI; 
import javax.ws.rs.core.UriBuilder; 

import org.glassfish.grizzly.http.server.HttpServer; 
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory; 
import com.sun.jersey.api.core.ClassNamesResourceConfig; 
import com.sun.jersey.api.core.ResourceConfig; 

public class Main { 

    private static URI getBaseURI() { 
     return UriBuilder.fromUri("http://localhost/").port(8080).build(); 
    } 

    public static final URI BASE_URI = getBaseURI(); 

    public static void main(String[] args) throws IOException { 
     ResourceConfig rc = new ClassNamesResourceConfig(LolCat.class); 
     HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc); 
     System.in.read(); 
     httpServer.stop(); 
    } 
} 

ve:

SEVERE: Missing dependency for field: javax.servlet.http.HttpServletRequest com.test.server.LolCat.hsr 

sunucusu, iki dosya, statik giriş noktasından (Main.java) basit ölü oluşur: Tüm süreç aşağıdaki hata ile yerine erken durak gibi görünüyor serlvet (LolCat.java):

package com.test.server; 

import javax.servlet.http.HttpServletRequest; 
import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.core.Context; 

@Path(value = "/lol") 
public class LolCat { 

    @Context HttpServletRequest hsr; 

    @GET 
    @Path(value="/cat") 
    public String list() { 
     return "meow"; 
    } 

} 

Özellikle, hepsi benim sorunlarına kaynak ve çözüm yukarıdaki kaynak dosyada @ Bağlam-hat. İhtiyacım var ve Jersey ve Servletler hakkında okuduğum her şeye göre çalışmalı, ama bunu yapmaz. Ayrıca GrizzlyServerFactory yerine GrizzlyWebContainerFactory kullanmayı denedim, ancak boşuna.

Başvuru için, proje aşağıdaki bağımlılıkları derlendi:

  • org.glassfish.grizzly: boz çerçevesi: kavanoz: 2.2.21
  • org.glassfish.grizzly: boz-http: kavanoz: org.glassfish.grizzly 2.2.21
  • : boz-http-servlet: kavanoz: 2.2.21
  • org.glassfish.grizzly: boz-http-sunucu: kavanoz: 2.2.21
  • com. sun.jersey: jersey-sunucu: jar: 1.17
  • com.sun.jersey: jarse-sunucu uygulaması: haznesi: 1.17
  • com.sun.jersey: jarse çekirdekli: haznesi: 1.17
  • javax.servlet: javax.servlet-api: haznesi: 2.5.0
  • com.sun.jersey: jarse-grizzly2: haznesi: 1.17
  • com.sun.jersey: jarse-grizzly2-sunucu uygulaması: haznesi: 1.17
  • asm: asm: haznesi: 3.3.1

cevap

3

Bu ana sınıf benim için iyi çalışıyor:

package com.test.server; 

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory; 
import java.io.IOException; 
import java.net.URI; 
import javax.ws.rs.core.UriBuilder; 

import com.sun.jersey.api.core.ClassNamesResourceConfig; 
import com.sun.jersey.spi.container.servlet.ServletContainer; 
import org.glassfish.grizzly.http.server.HttpHandler; 
import org.glassfish.grizzly.http.server.HttpServer; 
import org.glassfish.grizzly.http.server.Request; 
import org.glassfish.grizzly.http.server.Response; 
import org.glassfish.grizzly.servlet.ServletRegistration; 
import org.glassfish.grizzly.servlet.WebappContext; 

public class Main { 
    private static final String JERSEY_SERVLET_CONTEXT_PATH = ""; 

    private static URI getBaseURI() { 
     return UriBuilder.fromUri("http://localhost").port(8080).path("/").build(); 
    } 

    public static final URI BASE_URI = getBaseURI(); 

    public static void main(String[] args) throws IOException { 
     // Create HttpServer and register dummy "not found" HttpHandler 
     HttpServer httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, new HttpHandler() { 

      @Override 
      public void service(Request rqst, Response rspns) throws Exception { 
       rspns.setStatus(404, "Not found"); 
       rspns.getWriter().write("404: not found"); 
      } 
     }); 

     // Initialize and register Jersey Servlet 
     WebappContext context = new WebappContext("WebappContext", JERSEY_SERVLET_CONTEXT_PATH); 
     ServletRegistration registration = context.addServlet("ServletContainer", ServletContainer.class); 
     registration.setInitParameter(ServletContainer.RESOURCE_CONFIG_CLASS, 
       ClassNamesResourceConfig.class.getName()); 
     registration.setInitParameter(ClassNamesResourceConfig.PROPERTY_CLASSNAMES, LolCat.class.getName()); 
     registration.addMapping("/*"); 
     context.deploy(httpServer); 

     System.in.read(); 
     httpServer.stop(); 
    } 
} 

Tarayıcınızda http://localhost:8080/lol/cat deneyin. Servlet'in içerik yolunu güncellemek için JERSEY_SERVLET_CONTEXT_PATH değiştirebilirsiniz.

+0

Düz yazı tipi güvenliği ile çalıştı :). String/String setInitParameter kullanmadan bir sınıfa kayıt olmak mümkün mü? – Mic

+0

Ben Jersey'in GrizzlyWebContainerFactory genişletilmiş/geliştirilmiş olabilir init parametreleri ile başa çıkmak gerekmez, ancak yine de aynı WebappContext kayıt kodu üzerinde bir sarıcı olacak. – alexey

+0

Yardım! .. Kodum, WebappContext bağlamı = new WebappContext (... 'e kadar anlayacaktır, sonra aniden" thread özel durum "ana" java.lang.NoSuchMethodError: org.glassfish.grizzly.utils.DataStructures. getConcurrentMap (IFI) Ljava/util/eşzamanlı/ConcurrentMap; 'Java 7 ve 8 ile çalıştı, jersey-container-grizzly-http: 2.5 ve jersey-container-grizzly2-servlet kullanarak: 2.5 –

4

Geliştiricinin açıklamalarına göre - Grizzly, JAX-RS 2.0 ile tam uyumlu değildir, dolayısıyla resmi bağlamda enjeksiyon/paketleme olmayacaktır. Jersey + Grizzly sürümü 2.7+

için Jersey Bug-1960 Uygulanabilir bakınız Neyse ki Grizzly istek/yanıt nesneleri enjekte etmek bir yolu yoktur. Zor ama işe yarıyor Jersey örneğinden birinde verilen kod örneği.Bkz Jersey container test

Yani kod parçası olacak

:

import javax.inject.Inject; 
import javax.inject.Provider; 

public someclass { 
    @Inject 
    private Provider<Request> grizzlyRequestProvider; 

    public void method() { 
     if (grizzlyRequestProvider != null) { 
      Request httpRequest = grizzlyRequestProvider.get(); 

      // Extract what you need 
     } 
    } 
} 

yapılandırma sizin kaynaktır Nerede el ile de bir ResourceContext

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(getBaseURI()); 

    WebappContext context = new WebappContext("WebappContext", "/api"); 
    ServletRegistration registration = context.addServlet("ServletContainer", 
    new ServletContainer(config)); 
    registration.addMapping("/*"); 
    context.deploy(httpServer); 

kayıt olabilirsiniz

1

filtre ve servis yöntemleri için hem ince Works bağlamı.