0

我有一个 Spring Web 应用程序,这个应用程序有两个方面!一个是 Json Web 服务,另一个是静态视图。当我启动 Web 应用程序并使用 URL 转到视图时,它工作正常。但是当我尝试连接到 Json Web 服务时,服务器返回 404 错误。

这是我的 dispatcher-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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--  SPECIFIC CONFIGURATIONS -->
    <import resource="springConfigurations/common-config.xml"/>
    <import resource="springConfigurations/mvc-config.xml"/>

    <bean id="exceptionResolver" class="org.springframework.web.servlet.view.json.exception.JsonExceptionResolver">
        <property name="exceptionView">
            <value>jsonView</value>
        </property>
        <property name="errorHandler">
            <list>
                <ref bean="statusError" />
                <ref bean="modelFlagError" />
            </list>
        </property>
        <property name="exceptionHandler">
            <list>
                <ref bean="exceptionMessageExceptionHandler" />
                <ref bean="stackTraceExceptionHandler" />
            </list>
        </property>
    </bean>

    <bean name="exceptionMessageExceptionHandler" class="org.springframework.web.servlet.view.json.exception.ExceptionMessageExceptionHandler" />
    <bean name="stackTraceExceptionHandler" class="org.springframework.web.servlet.view.json.exception.StackTraceExceptionHandler"/>    

    <bean name="statusError" class="org.springframework.web.servlet.view.json.error.HttpStatusError" />
    <bean name="modelFlagError" class="org.springframework.web.servlet.view.json.error.ModelFlagError" />

</beans>

这是 common-config.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

        <!--  COMMON CONFIGURATIONS -->
        <mvc:annotation-driven/>
        <tx:annotation-driven/> 

        <context:component-scan base-package="com.jkcs.touchpos.application.controller" />

        <bean class="org.springframework.web.servlet.view.XmlViewResolver">
            <property name="location">
                <value>/WEB-INF/views.xml</value>
            </property>
            <property name="order" value="0" />
        </bean>

        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">
            <property name="order" value="1" />
        </bean>

        <!-- Annotations based Configuration -->
        <context:annotation-config />


        <!-- Components Auto-Detection (Backend) -->
        <context:component-scan base-package="com.jkcs.touchpos" use-default-filters="false" >
            <!-- Types annotated by Spring Managed, Controller and Transactional, or by an annotation that itself is 
            annotated by SpringManaged, Controller, Transactional -->
            <context:include-filter type="annotation" expression="com.jkcs.touchpos.platform.annotations.SpringManaged"/>
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.transaction.annotation.Transactional"/>
        </context:component-scan>

        <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

</beans>

Views.xml 文件

<beans xmlns="http://www.springframework.org/schema/beans"
    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-3.2.xsd">

    <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>

</beans>

服务等级。

@Controller
public class TouchPosService {

    @RequestMapping(value = "/service", method = RequestMethod.GET)
    public ModelAndView sampleJsonService(){

        Map<String, String> model = new HashMap<String, String>();
        model.put("Value", "Test Service to provide touchPos Data");

        return new ModelAndView("jasonView", model);
    }

}

我在这里做错了什么!我已经定义了两个具有特定优先级的视图解析器!但有些是怎么不工作的!请在这件事上给予我帮助!

谢谢你。

更新 :

服务器启动时,控制台会记录以下内容。

2013-04-22 13:40:51.384:INFO::Logging to StdErrLog::DEBUG=false via org.eclipse.jetty.util.log.StdErrLog
log4j:WARN No appenders could be found for logger (com.jkcs.touchpos.server.jetty.ServerRunner).
log4j:WARN Please initialize the log4j system properly.
2013-04-22 13:40:51.384:INFO::jetty-7.0.0.v20091005
2013-04-22 13:40:51.947:INFO:/:Initializing Spring FrameworkServlet 'dispatcher'
2013-04-22 13:40:53.509:INFO::Started SelectChannelConnector@0.0.0.0:9157

并且从浏览器中抛出以下 404 !

在此处输入图像描述

4

1 回答 1

1

您将视图映射为 jsonView<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>但稍后在控制器的 return 方法中,您将返回ModelAndView("jasonView", model);

这些都是一样的吗?我可以查看您是否在此处返回了错误的 jsp 文件名称,这会导致 404。

于 2013-04-22T08:19:36.607 回答