2017-12-21 26 views
0

스프링 -WebMVC 및 아파치 CXF를 사용하여 프로젝트를 설정하려고하지만 항상 웹 서비스를 호출하려고하면 "서비스를 찾을 수 없습니다."라는 메시지가 나타납니다.Spring WebMVC와 Apache CXF를 결합하려고하지만 서비스를 찾을 수 없습니다. 왜?

  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-디스패처 여기

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

    프로젝트 구성은 다음과 같습니다 아파치 톰캣 로그에서

    는 경고가 -servlet.xml

    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를 사용하고 0

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

  • . 평온한 웹 서비스처럼 보이는

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

    : :이 index.jsp에서 다음 코드를 포함

    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("https://stackoverflow.com/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; 
    
    
    } 
    

    내 스프링 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를 결합 할 수 있습니까? 아니면 이러한 프레임 워크가 하나의 프로젝트에서 호환되지 않습니까? 좀 더 자세한 정보가 필요하면 알려 주시면 제가 제공 할 것입니다. 귀하의 도움에 감사 드리며 미리 감사드립니다.

  • +0

    새로운 응용 프로그램입니까? 왜 오래된 XML 설정으로 귀찮아, 당신은 XML 무료 구성을 만들 수 있습니다. –

    +0

    나는 옛날 학교이고 나는 XML로 나의 구성을 좋아한다. 그럼에도 불구하고 이것은 내 주요 문제의 주제가 아닙니다 ... ;-) – F4k3d

    답변

    0

    2 개의 프로젝트에서 프로젝트를 분할했습니다. 하나의 프로젝트에는 웹 서비스가 포함되어 있고 다른 프로젝트에는 Spring WebMVC가 포함되어 있습니다. 이 문제를 해결할 다른 가능성이 있는지는 잘 모르겠지만 이런 식으로 작동합니다.