2

我正在开发一个小型 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。我做错了什么?

4

0 回答 0