我正在尝试结合使用 Spring WebMVC 和 Apache CXF,但找不到该服务。为什么?

I am trying to combine Spring WebMVC and Apache CXF but the service can't be find. Why?

我正在尝试使用 spring-WebMVC 和 apache CXF 设置一个项目,但如果我尝试调用 Web 服务,总是会出现一条消息告诉我:"No service was found."

在apache的tomcat日志中,有一个警告:

org.apache.cxf.transport.servlet.ServletController invoke
WARNING: Can't find the request for 
http://localhost:8080/HelloWebService/rs/user-service/users's Observer 

项目配置如下:

  1. web.xml:

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <display-name>RESTful service</display-name>
    <description></description>
    <session-config>
    <session-timeout>30</session-timeout>
    </session-config>
    
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>cxf.rest.root</param-value>
    </context-param>
    
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>
    </context-param>
    
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/appContext*.xml</param-value>
    </context-param>
    
    <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>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    
    
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/rs/*</url-pattern>
    </servlet-mapping>
    
    </web-app>
    
  2. mvc-调度程序-servlet.xml

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="org.home.playground" />
    <mvc:annotation-driven/>
    
    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    

  3. appContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
    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"
    default-lazy-init="false">
    
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    
    <jaxrs:server id="userService" address="/">
    <jaxrs:serviceBeans>
        <ref bean="userService" />
    </jaxrs:serviceBeans>
    <jaxrs:extensionMappings>
        <entry key="xml" value="application/xml" />
        <entry key="json" value="application/json" />
    </jaxrs:extensionMappings>
    </jaxrs:server>
    
    <bean id="userService" class="org.home.playground.services.UserService"/>
    
    </beans>
    

我正在使用这个 index.jsp 作为 webapp 的第一页。 index.jsp 包含以下代码:

<html>
<body>
    <h2>CXF RESTful services and Spring-WebMVC Test page</h2>
    <a href="rs/user-service/users">get all users</a>
    <br />
</body>
</html>

restful 网络服务如下所示:

package org.home.playground.interfaces;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.home.playground.apputils.UserCollection;
import org.home.playground.models.User;

@Path("/user-service/")
@Produces("application/xml")
public interface IUserService {

    @GET
    @Path("/users")
    @Produces({"application/xml", "application/json"})
    public UserCollection getUsers();

    @GET
    @Path("/user/{id}")
    public User getUser(@PathParam("id") Integer id);

    @GET
    @Path("/users/bad")
    public Response getBadRequest();

    @POST
    @Path("/new")
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public void newUser(
            @FormParam("id") Integer id,
            @FormParam("name") String name,
            @Context HttpServletResponse servletResponse
    ) throws IOException;


}

我的 Spring-webMVC 控制器看起来像这样:

package org.home.playground.web.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

    @RequestMapping("/")
    protected ModelAndView showHelloWorldView(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("index");

        return model;
    }
    @RequestMapping("/postUser")
    protected ModelAndView postUserForm(HttpServletRequest request, HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("postUser");

        return model;
    }
}

你知道为什么我不能调用网络服务吗?是否可以将 Spring-WebMVC 与 apache CXF 结合使用,或者这些框架在一个项目中不兼容? 如果您需要更多信息,请告诉我,我会提供。 非常感谢您的帮助,并提前致谢。

我已将项目拆分为 2 个项目。 一个项目包含 Web 服务内容,另一个项目包含 Spring WebMVC 内容。我不确定是否有另一种可能性来解决这个问题,但它是这样工作的。