is possible to combine: jersey + jetty + spring
I'm trying to create a webserver embedding jetty (rather than Java EE) , and map my servlets RESTfully, using jersey.
I'm using spring for dependency injection, and mapping the servlets as beans However, when I try to make an HTTP req to the mapped servlets, i get error 500- server error, or 404, page not found. I'm not sure if i'm doing this the right way, and I should probably be using the jetty.xml rather than this. (wondering if there's a shorcut using jetty.xml)
<bean id="contexts"
class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
</bean>
<bean id="server" class="org.mortbay.jetty.spring.Server"
init-method="start" destroy-method="stop">
<property name="threadPool">
<bean id="ThreadPool" class="org.eclipse.jetty.util.thread.QueuedThreadPool">
<property name="minThreads" value="10" />
<property name="maxThreads" value="50" />
</bean>
</property>
<property name="connectors">
<list>
<bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
<property name="port" value="8080" />
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<ref bean="contexts" />
<bean class="org.eclipse.jetty.server.handler.ResourceHandler">
<property name="directoriesListed" value="true" />
<property name="welcomeFiles">
<list>
<value>index.jsp</value>
</list>
</property>
<property name="resourceBase" value="./WebContent" />
</bean>
<bean id="myServletHandler" class="org.eclipse.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean id="jerseyServletContainer" class="org.eclipse.jetty.servlet.ServletHolder">
<property name="name" value="jersey" />
<property name="servlet">
<bean class="com.sun.jersey.spi.container.servlet.ServletContainer" />
</property>
<property name="initParameters">
<map>
<entry key="com.sun.jersey.config.property.resourceConfigClass"
value="com.sun.jersey.api.core.PackagesResourceConfig" />
<entry key="com.sun.jersey.config.property.packages"
value="servlets" />
</map>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean id="jerseyMapping" class="org.eclipse.jetty.servlet.ServletMapping">
<property name="servletName" value="jersey" />
<property name="pathSpec" value="/*" />
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
<property name="beans">
<list>
<bean id="ContextDeployer" class="org.eclipse.jetty.deploy.ContextDeployer">
<property name="contexts" ref="contexts" />
开发者_StackOverflow中文版 <property name="directory" value="contexts" />
<property name="scanInterval" value="5" />
</bean>
</list>
</property>
</bean>
I have one class in the servlets package: DoNothing.java
package servlets;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/nothing")
public class doNothing
{
@GET
@Produces("text/plain")
public String returnNothing()
{
return ("test");
}
}
what in the world am I doing wrong? or should I ask what in the world am I doing right?
Thanks
I was looking for a way to use Jersey+Spring+Embedded Jetty and found this question. I tried your method and it actually works.
If you want to actually use Spring beans in your resources you can use jersey-spring:
<bean id="server" class="org.mortbay.jetty.Server" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
<property name="port" value="8080"/>
</bean>
</list>
</property>
<property name="handlers">
<list>
<bean class="org.mortbay.jetty.servlet.Context">
<property name="contextPath" value="/"/>
<property name="sessionHandler">
<bean class="org.mortbay.jetty.servlet.SessionHandler" />
</property>
<property name="servletHandler">
<bean class="org.mortbay.jetty.servlet.ServletHandler">
<property name="servlets">
<list>
<bean class="org.mortbay.jetty.servlet.ServletHolder">
<property name="name" value="jersey" />
<property name="servlet">
<bean class="com.sun.jersey.spi.spring.container.servlet.SpringServlet" />
</property>
<property name="initParameters">
<map>
<entry key="com.sun.jersey.spi.container.ContainerRequestFilters"
value="com.sun.jersey.api.container.filter.LoggingFilter" />
<entry key="com.sun.jersey.spi.container.ContainerResponseFilters"
value="com.sun.jersey.api.container.filter.LoggingFilter" />
</map>
</property>
</bean>
</list>
</property>
<property name="servletMappings">
<list>
<bean class="org.mortbay.jetty.servlet.ServletMapping">
<property name="servletName" value="jersey"/>
<property name="pathSpecs">
<list>
<value>/*</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
<property name="eventListeners">
<list>
<bean id="requestContextListener" class="org.springframework.web.context.request.RequestContextListener"/>
<bean id="contextLoaderListener" class="org.springframework.web.context.ContextLoaderListener"/>
</list>
</property>
<property name="initParams">
<map>
<entry key="contextConfigLocation" value="classpath:META-INF/AdditionalBeansContext.xml"/>
</map>
</property>
</bean><!--
--></list>
</property>
</bean>
In file AdditionalBeansContext.xml:
Then define your Resources with: @Component annotation, injected beans with @Autowired
精彩评论