具有嵌入式jetty配置问题的jaxrs

时间:2013-03-13 05:15:47

标签: java rest jetty cxf jax-rs

我正在尝试使用 jaxrs jetty 来获取网络服务:

这是我的 jaxrms.xml 文件:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:cxf="http://cxf.apache.org/core"
        xmlns:jaxrs="http://cxf.apache.org/jaxrs"
        xsi:schemaLocation="
     http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <!-- import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" /-->
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="restService" class="com.as.rover.service.rest.RestService" >
    </bean>

    <jaxrs:server id="jaxrsRestService" address="/rest/">
        <jaxrs:serviceBeans>
            <ref bean="restService" />
        </jaxrs:serviceBeans>

    </jaxrs:server>



</beans>

这是我的 jetty.xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">



    <bean class="com.as.rover.service.JettyManager" factory-method="getInstance" id="jettyManager"> 
        <property name="server" >
            <bean id="jetty-server" class="org.eclipse.jetty.server.Server" init-method="start" destroy-method="stop">
                <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.HandlerList">
                    <property name="handlers">
                      <list>
                        <ref bean="servletContextHandler"></ref>
                        <!--bean class="org.eclipse.jetty.server.handler.ResourceHandler">
                          <property name="directoriesListed" value="true"/>
                          <property name="welcomeFiles">
                            <list>
                              <value>index.html</value>
                            </list>
                          </property>
                          <property name="resourceBase" value="."/>
                        </bean>       
                        <bean class="org.eclipse.jetty.server.handler.DefaultHandler"/-->
                        <!-- add more handlers here -->
                      </list>
                    </property>
                  </bean>
                </property>
              </bean>
        </property>
    </bean>
    <bean id="web-context" class="org.eclipse.jetty.webapp.WebAppContext">
                            <property name="resourceBase" value="./src/main/web"></property>
                            <property name="contextPath" value="/services/*"></property>    

    <bean id="servletContextHandler" class="org.eclipse.jetty.servlet.ServletContextHandler">
                            <property name="contextPath" value="/" />                                   
                        </bean>
    <bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
         <property name="targetObject" ref="servletContextHandler"/>
         <property name="targetMethod" value="addServlet"/>
         <property name="arguments">
           <list>
            <bean class="org.eclipse.jetty.servlet.ServletHolder">
                                                <property name="name" value="services" />
                                                <property name="servlet">
                                                    <bean class="org.apache.cxf.transport.servlet.CXFServlet"/>

                                                </property>
                                            </bean>
            <value>/</value>
           </list>
         </property>
        </bean>

        </beans>

我的休息服务类如下所示:

@Path("/test") // bind to versionnr in path  
public class RestService{

  @GET
  public long get() {
      return 1L;
  }
}

每当我向localhost:8080/services/test发出请求时,都会收到以下错误消息:

  

未找到服务。

我想用jaxrs配置我的嵌入式jetty服务器,但它似乎不起作用。我错误配置了码头吗?

1 个答案:

答案 0 :(得分:0)

如果您使用的是Spring,并且所有依赖项都处于合适状态,那么展示restful服务所需的唯一内容就是您的第一个文件。

cxf-rt-transports-http-jetty将负责您的服务代码和码头服务器之间的链接。

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd“&gt;

<context:annotation-config />

    <bean id="serviceImpl" class="com.as.rover.service.rest.serviceImpl" ></bean>

<!-- CXF -->
<import resource="classpath:META-INF/cxf/cxf.xml" />

<jaxrs:server id="JaxrsService" address="http://localhost:6066/services">
    <jaxrs:serviceBeans>
        <ref bean="serviceImpl" />
    </jaxrs:serviceBeans>
</jaxrs:server>

希望它有所帮助。