这里实际上我正在尝试访问基于Spring的休息全服务,我没有在web.xml中配置DispatcherServlet,而是使用ContxtLoaderListener来加载我的spring配置文件。
从我的日志中我可以看到我的服务正在初始化,当我访问上面的URL时,ICallServlet正在接收请求,因为它具有url-pattern为' / *'(我可以& #39; t修改)。
我的问题是我无法访问我的服务,请求未达到我的服务。没有使用DispatcherServlet有没有办法调用我的休息服务,有人请帮我解决这个问题。
我有一个Rest Controller:
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(No Dispatcher 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'
URL: http://localhost:8080/icall-ui/api/casaOnboarding/pwebXML
答案 0 :(得分:4)
对不起,但是你不能在没有Dispatcher Servlet的情况下发送spring mvc视图。您的上下文将通过ContextLoaderListener加载,但正如您已经发现的那样,您的路由将永远不会被调用。
您可以执行一些操作,例如将调度程序servlet映射到api端点,然后映射iCallUI以捕获默认路由&#34; /&#34;而不是&#34; / *&#34;:
<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指向另一个网址模式,例如:&#34; / ui / *&#34;。
这几乎耗尽了可能性:)。那个以及你的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 :(得分: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来访问服务
http://localhost:8080/icall-ui/api/api/casaOnboarding/pwebXML
答案 2 :(得分: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>