在没有Dispatcher Servlet的情况下访问Spring Rest服务

问题描述 投票:1回答:3

这里实际上我试图访问我的基于Spring的休息全服务,我没有在web.xml中配置DispatcherServlet,而不是我使用ContxtLoaderListener来加载我的spring配置文件。

从我的日志中我可以看到我的服务正在初始化,当我访问上面的URL时,ICallServlet正在接收请求,因为它的url-pattern为'/ *'(我无法修改)。

在这里我的问题是我无法访问我的服务,请求没有达到我的服务。没有使用DispatcherServlet有什么方法可以调用我的休息服务,有人请帮我解决这个问题。

  • 我有一个休息控制器: package mypackage; @RestController @RequestMapping("/api/casaOnboarding") public class CasaOnboardingRestService { @ResponseBody @RequestMapping(value="/pwebXML", method=RequestMethod.POST, consumes={"application/json", "application/xml"}) public ResponseEntity pwebXML(@RequestBody OnboardingReq onboardingReq, HttpServletRequest request, HttpServletResponse response){ System.out.println("Request Reached"); ---- } }
  • Web.xml(无调度程序Servlet) <?xml version="1.0" encoding="UTF-8"?> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:controllerServiceContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>iCallUI</servlet-name> <servlet-class>com.ui.ICallServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>iCallUI</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
  • controllerServiceContext.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <context:annotation-config /> <context:component-scan base-package="mypackage"/> <task:annotation-driven /> </beans>
  • 日志文件 10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Creating shared instance of singleton bean 'casaOnboardingRestService' 10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Creating instance of bean 'casaOnboardingRestService' 10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Eagerly caching bean 'casaOnboardingRestService' to allow for resolving potential circular references 10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Finished creating instance of bean 'casaOnboardingRestService'

网址:http://localhost:8080/icall-ui/api/casaOnboarding/pwebXML

spring spring-mvc spring-restcontroller spring-rest
3个回答
4
投票

对不起,但是如果没有Dispatcher Servlet,你就无法发送spring mvc视图。您的上下文将通过ContextLoaderListener加载,但正如您所发现的那样,您的路由将永远不会被调用。

您可以执行以下操作:将调度程序servlet映射到api端点,然后映射iCallUI以捕获默认路由“/”而不是“/ *”:

  <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/api/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>  
       <servlet-name>iCallUI</servlet-name>  
       <url-pattern>/</url-pattern>  
  </servlet-mapping>

ICallServlet将替换默认的servlet,这可能会或可能不会产生不良影响,具体取决于应用程序的设置方式。例如,静态文件服务可能会中断。

子类化org.springframework.web.servlet.DispatcherServlet是一个选项。但不知道你在com.ui.ICallServlet中做了什么,谁知道扩展DispatcherServlet会有多困难。

而且,似乎还有很长的路要走。如果您使用Spring来声明您的api路由,为什么不使用它来声明它们呢?为什么有两个调度机制?如果需要对每个请求进行一些预处理,请使用Servlet过滤器。

最后,也许是最简单的解决方案。只需将iCallUI指向另一个url模式,例如:“/ ui / *”。

这几乎耗尽了可能性:)。那个以及您的controllerServiceContext文件未设置为解析url映射的事实。你还需要添加

<mvc:annotation-driven />

不要忘记所有的xml命名空间信息!

  xmlns:mvc="http://www.springframework.org/schema/mvc"
  .
  .
   xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   .

1
投票

最后我知道在没有使用DispatcherServlet的情况下(根据我的知识)没有办法调用spring rest服务。

非常感谢@Robert提出的宝贵建议。根据@Robert评论,我修改了我的代码,如下所示,让它工作。

  • web.xml中 <?xml version="1.0" encoding="UTF-8"?> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:controllerServiceContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>iCallUI</servlet-name> <servlet-class>com.ui.ICallServlet</servlet-class> </servlet> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>iCallUI</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
  • 调度员servlet.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:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <context:component-scan base-package="mypackage"/> <mvc:annotation-driven /> </beans>
  • ControllerServiceContext.xml

我删除了下面的代码行,并保留旧代码(此文件包含一些与项目相关的其他内容)。

<context:component-scan base-package="mypackage"/>
<task:annotation-driven />
  • 日志文件

在日志中看到以下语句后,我可以说我的服务已准备好提供请求。

15:12:01,782 INFO  [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 58) Mapped "{[/api/casaOnboarding/pwebXML],methods=[POST],params=[],headers=[],consumes=[application/json || application/xml],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity mypackage.CasaOnboardingRestService.pwebXML(mypackage.OnboardingReq,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
  • URL - 我使用下面的url来访问该服务 http://localhost:8080/icall-ui/api/api/casaOnboarding/pwebXML

0
投票

通过在web.xml中使用过滤器

    <!-- Spring security -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>
                  org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
© www.soinside.com 2019 - 2024. All rights reserved.