Hi,
I use Jersey 1.3 and jersey-guice in maven2
java doc from this URL:
https://jersey.dev.java.net/nonav/apidocs/latest/contribs/jersey-guice/com/sun/jersey/guice/spi/container/servlet/package-summary.html
Jersey's samples bookstore works fine, when not use guice.
My listener is like this:
public class MyGuiceConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
bind(Login.class);
Map<String, String> params = new HashMap<String, String>();
// com.sun.jersey.config.feature.Redirect
params.put(ResourceConfig.FEATURE_REDIRECT, "true");
// com.sun.jersey.config.feature.ImplicitViewables
params.put(ResourceConfig.FEATURE_IMPLICIT_VIEWABLES, "true");
// com.sun.jersey.config.property.WebPageContentRegex
params.put(ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX,
"/(images|css|jsp)/.*");
// com.sun.jersey.config.property.packages
params.put(PackagesResourceConfig.PROPERTY_PACKAGES,
"com.sun.jersey.samples.bookstore.resources");
serve("/*").with(GuiceContainer.class, params);
}
});
}
}
If don't bind something. will show some exception.
com.sun.jersey.api.container.ContainerException: The ResourceConfig instance
does not contain any root resource classes.
at
com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:100)
at
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1013)
at
com.sun.jersey.server.impl.application.WebApplicationImpl.access$600(WebApplicationImpl.java:153)
at
com.sun.jersey.server.impl.application.WebApplicationImpl$11.f(WebApplicationImpl.java:652)
at
com.sun.jersey.server.impl.application.WebApplicationImpl$11.f(WebApplicationImpl.java:649)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:177)
at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:649)
at
com.sun.jersey.guice.spi.container.servlet.GuiceContainer.initiate(GuiceContainer.java:115)
then I bind a simple class Login.class
and can not use filter like this:
filter("/*").through(ServletContainer.class, params);
this is javax.servlet.ServletException,
Filters must be bound as singletons.
Key[type=com.sun.jersey.spi.container.servlet.ServletContainer,
annotation=[none]] was not bound in singleton scope.
Then use serve("/*").with(GuiceContainer.class, params);
There is no exception,bbut it did not work for jsp template.
By the way, it starts very slow, during maven jetty:run. but did not have
any exception.
here is my jetty-maven plugin, when use jersey-guice, it took a long time to
start.
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.5.v20100705</version>
<configuration>
<systemProperties>
<systemProperty>
<name>logback.configurationFile</name>
<value>./src/main/resources/logback.xml</value>
</systemProperty>
</systemProperties>
<connectors>
<connector
implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${http.port}</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>${jetty-contextPath}</contextPath>
</webAppConfig>
<stopPort>9966</stopPort>
<stopKey>stop</stopKey>
<requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">
<filename>./target/yyyy_mm_dd.request.log</filename>
<retainDays>90</retainDays>
<append>true</append>
<extended>true</extended>
<logTimeZone>GMT</logTimeZone>
</requestLog>
</configuration>
</plugin>
here is console log
2010-07-25 16:51:42.186:INFO::jetty-7.1.5.v20100705
2010-07-25 16:51:43.613:INFO::No Transaction manager found - if your webapp
requires one, please configure one.
2010-07-25 16:53:20.253:WARN::Skipping PostConstruct annotation on
com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.init: has parameters
2010-7-25 16:53:21 com.sun.jersey.api.core.PackagesResourceConfig init
信息: Scanning for root resource and provider classes in the packages:
com.sun.jersey.samples.bookstore.resources
2010-7-25 16:53:21
com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory register
信息: Registering foo.Login as a root resource class
2010-7-25 16:53:21 com.sun.jersey.server.impl.application.WebApplicationImpl
_initiate
信息: Initiating Jersey application, version 'Jersey: 1.3 06/17/2010 04:53 PM'
2010-7-25 16:53:22
com.sun.jersey.guice.spi.container.GuiceComponentProviderFactory
getComponentProvider
信息: Binding foo.Login to GuiceManagedComponentProvider with the scope
"PerRequest"
2010-07-25 16:53:22.498:INFO::Opened
C:\opt\eclipse-SDK-3.6-win32\workspace\foo\target\2010_07_25.request.log
[INFO] Started Jetty Server
2010-07-25 16:53:22.514:INFO::Started SelectChannelConnector_at_0.0.0.0:80
[INFO] Starting scanner at interval of 10 seconds.
during these tow lines:
2010-07-25 16:51:43.613:INFO::No Transaction manager found - if your webapp
requires one, please configure one.
2010-07-25 16:53:20.253:WARN::Skipping PostConstruct annotation on
com.sun.jersey.server.impl.ejb.EJBInjectionInterceptor.init: has parameters
It takes about 2 mins. and show me a warn. and CPU use about 50% and 150M
memory.
This is too large cost during development.
Any idea? Thanks a lot!
Zhanming
--
View this message in context: http://jersey.576304.n2.nabble.com/Jersey-Guice-JSP-how-to-tp5334589p5334589.html
Sent from the Jersey mailing list archive at Nabble.com.