如何使用注释配置JAXRS和Spring

时间:2013-08-19 17:03:08

标签: java spring annotations jetty

我正在开发一个小型的Spring Web服务项目。这是一个示例路线:

package com.example.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.PathParam;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.core.MediaType;

@Provider
public class WebServiceRoutes {

    @GET
    @Path("/hello")
    public String health() {
        return "Hello";
    }

}

然后在我的Spring配置类中:

package com.example.api;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringContextConfiguration {

    @Bean( destroyMethod = "shutdown" )
    public SpringBus cxf() {
        return new SpringBus();
    }

    @Bean
    public WebServiceRoutes webServiceRoutes() {
        return new WebServiceRoutes();
    }
}

最后在我的web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0">
    <display-name>api</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.example.api.SpringContextConfiguration</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

但是,当我在jetty中将其部署为war文件,并尝试请求/ api / hello时,我收到消息:“找不到服务。”状态为404.我做错了什么?

0 个答案:

没有答案