可以组合:球衣 +码头 +春天

发布于 2024-09-06 18:27:45 字数 4817 浏览 6 评论 0原文

我正在尝试创建一个嵌入 jetty(而不是 Java EE)的 Web 服务器,并使用 jersey 以 REST 方式映射我的 servlet。
我使用 spring 进行依赖注入,并将 servlet 映射为 beans 但是,当我尝试对映射的 servlet 发出 HTTP 请求时,我收到错误 500-服务器错误,或 404,找不到页面。
我不确定我是否以正确的方式这样做,我可能应该使用 jetty.xml 而不是这个。 (想知道是否有使用 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" />
                <property name="directory" value="contexts" />
                <property name="scanInterval" value="5" />
            </bean>
        </list>
    </property>
</bean>

我的 servlets 包中有一个类: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");
    }


}

我到底做错了什么?或者我应该问我到底做对了什么?

谢谢

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" />
                <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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-09-13 18:27:45

我正在寻找一种使用 Jersey+Spring+Embedded Jetty 的方法,并发现了这个问题。我尝试了你的方法,确实有效。

如果你想在你的资源中实际使用Spring bean,你可以使用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>

在文件AdditionalBeansContext.xml中:

然后使用 @Component 注释定义您的资源,使用 @Autowired 注入 bean

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文